Home » Express.js Response

Express.js Response

by Online Tutorials Library

Express.js Response Object

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:

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:

Response Attachment method

Syntax:

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

Examples:

Response Cookie method

Syntax:

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

Examples:

Response ClearCookie method

Syntax:

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

Examples:

To set a cookie

To clear a cookie:

Response Download method

Syntax:

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

Example:

Response End method

Syntax:

This method is used to end the response process.

Example:

Response Format method

Syntax:

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

Example:

Response Get method

Syntax:

This method provides HTTP response header specified by field.

Example:

Response JSON method:

Syntax:

This method returns the response in JSON format.

Example:

Response JSONP method

Syntax:

This method returns response in JSON format with JSONP support.

Examples:

Response Links method

Syntax:

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

Examples:

Response Location method

Syntax:

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

Examples:

Response Redirect method

Syntax:

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

Examples:

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

You may also like