Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "res" and "req" parameters in Express functions?

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!

like image 916
expressnoob Avatar asked Jan 14 '11 21:01

expressnoob


People also ask

What is req params in Express?

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.

What is RES in Express JS?

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.

What is RES on in Nodejs?

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.

What is the use of REQ params?

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.


2 Answers

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 triggered
  • request.method, which will be "GET" in this case, hence the app.get() call.
  • An array of HTTP headers in 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.
  • An array of query string parameters if there were any, in 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); }); 
like image 168
Dave Ward Avatar answered Oct 02 '22 16:10

Dave Ward


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? )

like image 39
Myrne Stol Avatar answered Oct 02 '22 15:10

Myrne Stol