Express.js Response

The Response object (res) specifies the HTTP response which is sent by an Express app when it gets an HTTP request.

What it does

  • It sends response back to the client browser.
  • It facilitates you to put new cookies value and that will write to the client browser (under cross domain rule).
  • Once you res.send() or res.redirect() or res.render(), you cannot do it again, otherwise, there will be uncaught error.

Response Object Properties

Let's see some properties of response object.

Index Properties Description
1. res.app It holds a reference to the instance of the express application that is using the middleware.
2. res.headersSent It is a Boolean property that indicates if the app sent HTTP headers for the response.
3. res.locals It specifies an object that contains response local variables scoped to the request

Response Object Methods

Following are some methods:

Response Append method

Syntax:

snippet
res.append(field [, value])

This method appends the specified value to the HTTP response header field. That means if the specified value is not appropriate then this method redress that.

Examples:

snippet
res.append('Link', ['', '']);
res.append('Warning', '199 Miscellaneous warning');

Response Attachment method

Syntax:

snippet
res.attachment([filename])

This method facilitates you to send a file as an attachment in the HTTP response.

Examples:

snippet
res.attachment('path/to/js_pic.png');

Response Cookie method

Syntax:

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

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

Examples:

snippet
res.cookie('name', 'Aryan', { domain: '.xyz.com', path: '/admin', secure: true });
res.cookie('Section', { Names: [Aryan,Sushil,Priyanka] });
res.cookie('Cart', { items: [1,2,3] }, { maxAge: 900000 });

Response ClearCookie method

Syntax:

snippet
res.clearCookie(name [, options])

As the name specifies, the clearCookie method is used to clear the cookie specified by name.

Examples:

To set a cookie

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

To clear a cookie:

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

Response Download method

Syntax:

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

This method transfers the file at path as an "attachment" and enforces the browser to prompt user for download.

Example:

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

Response End method

Syntax:

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

This method is used to end the response process.

Example:

snippet
res.end();
res.status(404).end();

Response Format method

Syntax:

snippet
res.format(object)

This method performs content negotiation on the Accept HTTP header on the request object, when present.

Example:

snippet
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');
  }
});

Response Get method

Syntax:

snippet
res.get(field)

This method provides HTTP response header specified by field.

Example:

snippet
res.get('Content-Type');

Response JSON method:

Syntax:

snippet
res.json([body])

This method returns the response in JSON format.

Example:

snippet
res.json(null)
res.json({ name: 'ajeet' })

Response JSONP method

Syntax:

snippet
res.jsonp([body])

This method returns response in JSON format with JSONP support.

Examples:

snippet
res.jsonp(null)
res.jsonp({ name: 'ajeet' })

Response Links method

Syntax:

snippet
res.links(links)

This method populates the response?s Link HTTP header field by joining the links provided as properties of the parameter.

Examples:

snippet
res.links({
  next: 'http://api.rnd.com/users?page=5',
  last: 'http://api.rnd.com/users?page=10'
});

Response Location method

Syntax:

snippet
res.location(path)

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

Examples:

snippet
res.location('http://xyz.com');

Response Redirect method

Syntax:

snippet
res.redirect([status,] path)

This method redirects to the URL derived from the specified path, with specified HTTP status

Examples:

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

Response Render method

Syntax:

This method renders a view and sends the rendered HTML string to the client.

Examples:

Response Send method

Syntax:

This method is used to send HTTP response.

Examples:

Response sendFile method

Syntax:

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

Examples:

Response Set method

Syntax:

This method is used to set the response of HTTP header field to value.

Examples:

Response Status method

Syntax:

This method sets an HTTP status for the response.

Examples:

Response Type method

Syntax:

This method sets the content-type HTTP header to the MIME type.

Examples:

Next TopicExpressJS GET




Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +