/RESTful-API

Express & Node.js to create a sleek CRUD RESTful API.

Primary LanguageJavaScript

RESTful-API

Express & Node.js to create a sleek CRUD RESTful API. Models for payload validation to casually throw 400 client side errors💁‍♀️

MongoDB Postman Studio VS JSON Express

What is difference between req.query & req.params in mongoose controllers?

.params only get the route parameters while .query gets query strings passed.

.query

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)));
}

.params

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)));
}

Resources:

  1. https://mongoosejs.com/docs/tutorials/findoneandupdate.html
  2. https://www.geeksforgeeks.org/mongoose-findoneandupdate-function/