request.body vs request.params vs request.query

16,422

req.params is route parameters, req.body is the actual body of the request, and req.query is any query parameters.

For example, if I declare this route:

router.get('/user/:id', function(req, res) {});

req.params will contain id.

If I pass a body to this route:

{
  name: 'josh'
}

This will be in req.body.

If I pass some query parameters to http://myserver.com/api/user?name="josh", req.query will be { name: 'josh' }.

Check out the Express docs.

Share:
16,422
Ming Huang
Author by

Ming Huang

Front-End Developer, enjoys coding and learning new Javascript frameworks.

Updated on July 27, 2022

Comments

  • Ming Huang
    Ming Huang almost 2 years

    I have a client side JS file that has:

    agent = require('superagent'); request = agent.get(url);

    Then I have something like

    request.get(url) 
    //or
    request.post(url)
    request.end( function( err, results ) {
            resultCallback( err, results, callback );
        } );
    

    On the backend Node side I have request.body and request.params and some has request.query

    What are the difference between the body, params and query?