In the following Express function:
app.get('/user/:id', function(req, res){ res.send('user' + req.params.id); });
What are req
and res
? What do they stand for, what do they mean, and what do they do?
Thanks!
params is an object of the req object that contains route parameters. If the params are specified when a URL is built, then the req. params object will be populated when the URL is requested.
Short for response , the res object is one half of the request and response cycle to send data from the server to the client-side through HTTP requests.
res. on('data', ...) is how you register a listener for the data event and the data event is the primary way that you receive data from the incoming stream. This listener will be called one or more times with chunks of arriving data.
req. param() searches the URL path, body, and query string of the request (in that order) for the specified parameter. If no parameter value exists anywhere in the request with the given name , it returns undefined or the optional defaultValue if specified.
req
is an object containing information about the HTTP request that raised the event. In response to req
, you use res
to send back the desired HTTP response.
Those parameters can be named anything. You could change that code to this if it's more clear:
app.get('/user/:id', function(request, response){ response.send('user ' + request.params.id); });
Edit:
Say you have this method:
app.get('/people.json', function(request, response) { });
The request will be an object with properties like these (just to name a few):
request.url
, which will be "/people.json"
when this particular action is triggeredrequest.method
, which will be "GET"
in this case, hence the app.get()
call.request.headers
, containing items like request.headers.accept
, which you can use to determine what kind of browser made the request, what sort of responses it can handle, whether or not it's able to understand HTTP compression, etc.request.query
(e.g. /people.json?foo=bar
would result in request.query.foo
containing the string "bar"
).To respond to that request, you use the response object to build your response. To expand on the people.json
example:
app.get('/people.json', function(request, response) { // We want to set the content-type header so that the browser understands // the content of the response. response.contentType('application/json'); // Normally, the data is fetched from a database, but we can cheat: var people = [ { name: 'Dave', location: 'Atlanta' }, { name: 'Santa Claus', location: 'North Pole' }, { name: 'Man in the Moon', location: 'The Moon' } ]; // Since the request is for a JSON representation of the people, we // should JSON serialize them. The built-in JSON.stringify() function // does that. var peopleJSON = JSON.stringify(people); // Now, we can use the response object's send method to push that string // of people JSON back to the browser in response to this request: response.send(peopleJSON); });
I noticed one error in Dave Ward's answer (perhaps a recent change?): The query string paramaters are in request.query
, not request.params
. (See https://stackoverflow.com/a/6913287/166530 )
request.params
by default is filled with the value of any "component matches" in routes, i.e.
app.get('/user/:id', function(request, response){ response.send('user ' + request.params.id); });
and, if you have configured express to use its bodyparser (app.use(express.bodyParser());
) also with POST'ed formdata. (See How to retrieve POST query parameters? )
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