Express & Node.js to create a sleek CRUD RESTful API. Models for payload validation to casually throw 400 client side errors💁♀️
.params only get the route parameters while .query gets query strings passed.
GET localhost:3000/contact/contactId/?contactId=(Id)
crmRoutes.js
app.route("/contact/contactId")
.get(getContactById)
crmControllers.js
export const getContactById = (req, res) => {
const Id = req.query.contactId
console.log(Id)
const name = req.query.fname
Contact.findById(Id).then(contact => res.json(contact)).catch(err => (console.log(err)));
}
GET localhost:3000/contact/(Id)
crmRoutes.js
app.route("/contact/:contactId")
crmControllers.js
export const getContactById = (req, res) => {
const Id = req.params.contactId
console.log(Id)
Contact.findById(Id).then(contact => res.json(contact)).catch(err => (console.log(err)));
}