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?
req. query contains the query params of the request. req. body contains anything in the request body.
Usually the content body is used for the data that is to be uploaded/downloaded to/from the server and the query parameters are used to specify the exact data requested. For example when you upload a file you specify the name, mime type, etc.
You can use QueryParameter to ignore form values, while you'd want to use RequestParameter if you need to read the values from a form post. RequestParameter may return more values than QueryParameter, especially if the request is a form post.
A request body is data sent by the client to your API. A response body is the data your API sends to the client. Your API almost always has to send a response body. But clients don't necessarily need to send request bodies all the time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With