HI WELCOME TO SIRIS

Express.js Response Object

Leave a Comment
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.
IndexPropertiesDescription
1.res.appIt holds a reference to the instance of the express application that is using the middleware.
2.res.headersSentIt is a Boolean property that indicates if the app sent HTTP headers for the response.
3.res.localsIt specifies an object that contains response local variables scoped to the request

Response Object Methods

Following are some methods:

Response Append method

Syntax:
  1. 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:
  1. res.append('Link', ['<http://localhost/>''<http://localhost:3000/>']);  
  2. res.append('Warning''199 Miscellaneous warning');  

Response Attachment method

Syntax:
  1. res.attachment([filename])  
This method facilitates you to send a file as an attachment in the HTTP response.
Examples:
  1. res.attachment('path/to/js_pic.png');    

Response Cookie method

Syntax:
  1. 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:
  1. res.cookie('name''Kansiris', { domain: '.xyz.com', path: '/admin', secure: true });  
  2. res.cookie('Section', { Names: [Kansiris,Sushil,Priyanka] });  
  3. res.cookie('Cart', { items: [1,2,3] }, { maxAge: 900000 });  

Response ClearCookie method

Syntax:
  1. 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
  1. res.cookie('name', 'Kansiris', { path: '/admin' });  
To clear a cookie:
  1. res.clearCookie('name', { path: '/admin' });  

Response Download method

Syntax:
  1. 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:
  1. res.download('/report-12345.pdf');  

Response End method

Syntax:
  1. res.end([data] [, encoding])   
This method is used to end the response process.
Example:
  1. res.end();  
  2. res.status(404).end();  

Response Format method

Syntax:
  1. res.format(object)   
This method performs content negotiation on the Accept HTTP header on the request object, when present.
Example:
  1. res.format({  
  2.   'text/plain': function(){  
  3.     res.send('hey');  
  4.   },  
  5.  'text/html': function(){  
  6.     res.send('  
  7. hey');  
  8.   },  
  9.   'application/json': function(){  
  10.     res.send({ message: 'hey' });  
  11.   },  
  12.  'default': function() {  
  13.     // log the request and respond with 406  
  14.     res.status(406).send('Not Acceptable');  
  15.   }  
  16. });  

Response Get method

Syntax:
  1. res.get(field)   
This method provides HTTP response header specified by field.
Example:
  1. res.get('Content-Type');   

Response JSON method:

Syntax:
  1. res.json([body])   
This method returns the response in JSON format.
Example:
  1. res.json(null)  
  2. res.json({ name: 'ajeet' })  

Response JSONP method

Syntax:
  1. res.jsonp([body])   
This method returns response in JSON format with JSONP support.
Examples:
  1. res.jsonp(null)  
  2. res.jsonp({ name: 'ajeet' })  

Response Links method

Syntax:
  1. res.links(links)   
This method populates the response?s Link HTTP header field by joining the links provided as properties of the parameter.
Examples:
  1. res.links({  
  2.   next: 'http://api.rnd.com/users?page=5',  
  3.   last: 'http://api.rnd.com/users?page=10'  
  4. });  

Response Location method

Syntax:
  1. res.location(path)   
This method is used to set the response location HTTP header field based on the specified path parameter.
Examples:
  1. res.location('http://xyz.com');  

Response Redirect method

Syntax:
  1. res.redirect([status,] path)   
This method redirects to the URL derived from the specified path, with specified HTTP status
Examples:
  1. res.redirect('http://example.com');  

Response Render method

Syntax:
  1. res.render(view [, locals] [, callback])  
This method renders a view and sends the rendered HTML string to the client.
Examples:
  1. // send the rendered view to the client  
  2. res.render('index');  
  3. // pass a local variable to the view  
  4. res.render('user', { name: 'Kansiris' }, function(err, html) {  
  5.   // ...  
  6. });  

Response Send method

Syntax:
  1. res.send([body])   
This method is used to send HTTP response.
Examples:
  1. res.send(new Buffer('whoop'));  
  2. res.send({ some: 'json' });  
  3. res.send('  
  4. .....some html  
  5. ');  

Response sendFile method

Syntax:
  1. res.sendFile(path [, options] [, fn])   
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:
  1. res.sendFile(fileName, options, function (err) {  
  2.   // ...  
  3. });  

Response Set method

Syntax:
  1. res.set(field [, value])   
This method is used to set the response of HTTP header field to value.
Examples:
  1. res.set('Content-Type', 'text/plain');  
  2.   
  3. res.set({  
  4.   'Content-Type': 'text/plain',  
  5.   'Content-Length': '123',  
  6. })  

Response Status method

Syntax:
  1. res.status(code)   
This method sets an HTTP status for the response.
Examples:
  1. res.status(403).end();  
  2. res.status(400).send('Bad Request');  

Response Type method

Syntax:
  1. res.type(type)   
This method sets the content-type HTTP header to the MIME type.
Examples:
  1. res.type('.html');              // => 'text/html'  
  2. res.type('html');               // => 'text/html'  
  3. res.type('json');               // => 'application/json'  
  4. res.type('application/json');   // => 'application/json'  
  5. res.type('png');                // => image/png:  

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.