Example Object response of express

The res object represents the HTTP response that an Express app sends when it gets an HTTP request.

Response Object Properties

1. res.app: This property holds a reference to the instance of the express application that is using the middleware.

2. res.headersSent: Boolean property that indicates if the app sent HTTP headers for the response.

3. res.locals: An object that contains response local variables scoped to the request

Response Object Methods

1.res.append(field [, value])

This method appends the specified value to the HTTP response header field.

Example:

 

res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);

res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');

res.append('Warning', '199 Miscellaneous warning');

 

2. res.attachment([filename])

This method is used to send a file as an attachment in the HTTP response.

Example:

 

res.attachment('path/to/logo.png');

 

3.res.cookie(name, value [, options])

This method is used to set cookie name to value. The value parameter may be a string or object converted to JSON.

Example:

 

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });

res.cookie('cart', { items: [1,2,3] });

res.cookie('cart', { items: [1,2,3] }, { maxAge: 900000 });

 

4. res.clearCookie(name [, options])

This method is used to clear the cookie specified by name.

Example:

 

res.cookie('name', 'tobi', { path: '/admin' });

res.clearCookie('name', { path: '/admin' });

 

5. res.download(path [, filename] [, fn])

This method is used to transfer the file at path as an "attachment". Typically, browsers will prompt the user for download.

Example:

 

res.download('/report-12345.pdf');

res.download('/report-12345.pdf', 'report.pdf');

res.download('/report-12345.pdf', 'report.pdf', function(err){

 

});

 

6. res.end([data] [, encoding])

This method is used to end the response process.

Example:

 

res.end();

res.status(404).end();

 

7.res.format(object)

This method is used to perform content-negotiation on the Accept HTTP header on the request object, when present.

Example:

 

res.format({

   'text/plain': function(){

      res.send('hey');

   },

 

   'text/html': function(){

      res.send('hey');

   },

 

   'application/json': function(){

      res.send({ message: 'hey' });

   },

 

   'default': function() {

      // log the request and respond with 406

      res.status(406).send('Not Acceptable');

   }

});

 

8. res.get(field)

This method is used to return the HTTP response header specified by field.

Example:

 

res.get('Content-Type');

 

9. res.json([body])

This method is used to send a JSON response.

Example:

 

res.json(null)

res.json({ user: 'tobi' })

res.status(500).json({ error: 'message' })

 

10. res.jsonp([body])

This method is used to send a JSON response with JSONP support.

Example:

 

res.jsonp(null)

res.jsonp({ user: 'tobi' })

res.status(500).jsonp({ error: 'message' })

 

11. res.links(links)

This method is used to join the links provided as properties of the parameter to populate the response’s Link HTTP header field.

Example:

 

res.links({

   next: 'http://api.example.com/users?page=2',

   last: 'http://api.example.com/users?page=5'

});

 

12. res.location(path)

This method is used to set the response Location HTTP header field based on the specified path parameter.

Example:

 

res.location('/foo/bar');

res.location('foo/bar');

res.location('http://example.com');

 

13. res.redirect([status,] path)

This method is used to redirect to the URL dervied from the specified path, with specified HTTP status code status.

Example:

 

res.redirect('/foo/bar');

res.redirect('http://example.com');

res.redirect(301, 'http://example.com');

 

14. res.render(view [, locals] [, callback])

This method is used to render a view and sends the rendered HTML string to the client.

Example:

 

// send the rendered view to the client

res.render('index');

// pass a local variable to the view

res.render('user', { name: 'Tobi' }, function(err, html) {

   // ...

});

 

15. res.send([body])

This method is used to send the HTTP response.

Example:

 

res.send(new Buffer('whoop'));

res.send({ some: 'json' });

res.send('some html');

 

16. res.sendFile(path [, options] [, fn])

This method is used to transfer the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension.

Example:

 

res.sendFile(fileName, options, function (err) {

   // ...

});

 

17. res.sendStatus(statusCode)

This method is used to set the response HTTP status code to statusCode and send its string representation as the response body.

Example:

 

res.sendStatus(200); // equivalent to res.status(200).send('OK')

res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')

res.sendStatus(404); // eq