Nodejs - Day 06
Nodejs - Day 06
● We are now creating a POST method to save menu details and it’s similar
to person details and the same for the GET method
https://drive.google.com/file/d/1TswAyCgfsa04Hp6f4OP-Umg_GVkdW4eQ/view?
usp=sharing
● Now if someone told you to give a list of people who are only waiters
● Then we can create an endpoint like this
● /person/chef
● /person/waiter
● /person/manager
● But this is not the correct method to create as many functions Here we can
use parametrized endpoints
● It can be dynamically inserted into the URL when making a request to the
API.
● localhost:3000/person/:work
module.exports = router;
● Now in server.js, we will use this personRoutes
// Import the router files
const personRoutes = require('./routes/personRoutes');
● Update Operation
● We will update our person Records, and for that, we will create an endpoint
from where we are able to update the record
● For Updation, we need two things
○ Which record we want to update?
○ What exactly do we want to update?
● For update, we will use the PUT method to create an endpoint
● What is a unique identifier in a document in a collection?
● It’s _id which Mongodb itself gives, We will use this to find the particular
record that we want to update
● —> And now we will send the data the same as we did in the POST
method.
if (!updatedPerson) {
return res.status(404).json({ error: 'Person not found'
});
}
● Delete Operation
● We will Delete our person Records, and for that we will create an endpoint
from where we are able to delete the record
● For Deletion, we need one thing
○ Which record we want to update?
● For deletion, we will use the DELETE method to create an endpoint
● What is a unique identifier in a document in a collection?
● It’s _id which Mongodb itself gives, We will use this to find the particular
record that we want to delete
if (!deletedPerson) {
return res.status(404).json({ error: 'Person not found' });
}