The Response object (res) specifies the HTTP response which is sent by an Express app when it gets an HTTP request.
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 |
Following are some methods:
Syntax:
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:
res.append('Link', [' ', ' ']);
res.append('Warning', '199 Miscellaneous warning');Syntax:
res.attachment([filename])
This method facilitates you to send a file as an attachment in the HTTP response.
Examples:
res.attachment('path/to/js_pic.png');Syntax:
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:
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 });Syntax:
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
res.cookie('name', 'Aryan', { path: '/admin' });To clear a cookie:
res.clearCookie('name', { path: '/admin' });Syntax:
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:
res.download('/report-12345.pdf');Syntax:
res.end([data] [, encoding])
This method is used to end the response process.
Example:
res.end(); res.status(404).end();
Syntax:
res.format(object)
This method performs 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');
}
});Syntax:
res.get(field)
This method provides HTTP response header specified by field.
Example:
res.get('Content-Type');Syntax:
res.json([body])
This method returns the response in JSON format.
Example:
res.json(null)
res.json({ name: 'ajeet' })Syntax:
res.jsonp([body])
This method returns response in JSON format with JSONP support.
Examples:
res.jsonp(null)
res.jsonp({ name: 'ajeet' })Syntax:
res.links(links)
This method populates the response?s Link HTTP header field by joining the links provided as properties of the parameter.
Examples:
res.links({
next: 'http://api.rnd.com/users?page=5',
last: 'http://api.rnd.com/users?page=10'
});Syntax:
res.location(path)
This method is used to set the response location HTTP header field based on the specified path parameter.
Examples:
res.location('http://xyz.com');Syntax:
res.redirect([status,] path)
This method redirects to the URL derived from the specified path, with specified HTTP status
Examples:
res.redirect('http://example.com');Syntax:
This method renders a view and sends the rendered HTML string to the client.
Examples:
Syntax:
This method is used to send HTTP response.
Examples:
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:
Syntax:
This method is used to set the response of HTTP header field to value.
Examples:
Syntax:
This method sets an HTTP status for the response.
Examples:
Syntax:
This method sets the content-type HTTP header to the MIME type.
Examples: