Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Response Object in Node.js: Tips and Examples

Understand Response Object in Node.js with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and...

Table of Contents

Response Object in Node.js is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

The object res represents the HTTP Response that the Express application sends when receiving an HTTP Request.

Properties of the Response object in Node.js

The following table lists some properties of the Response object in Node.js.

Stt Properties & Description1 res.app This property holds a reference to the Express application that is using Middleware. 2 res.headersSent Value true, false to indicate whether the application has sent the HTTP Header for Response. 3 res.locals An object contains local variables of Response in the Request range.

Method of Response object in Node.js

Method res.append (field [, value])

 res . append ( field [, value ]) 

This method appends a specific value to the HTTP Header of Response field. As an example,:

 res . append ( 'Link' , [ ' ' , ' ' ]); res . append ( 'Set-Cookie' , 'foo=bar; Path=/; HttpOnly' ); res . append ( 'Warning' , 'Qua nhieu thong tin hon tap' ); 

Res.attachment ([filename]) method

 res . attachment ([ filename ]) 

This method helps send a File as an Attachment in the HTTP Response. As an example,:

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

Res.cookie method (name, value [, options])

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

This method helps set the name of Cookie to value. The value parameter can be a string or an object converted to JSON. As an 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 }); 

Res.clearCookie method (name [, options])

 res . clearCookie ( name [, options ]) 

This method helps delete specific cookies specified by the name parameter. As an example,:

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

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

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

This method helps transfer the file in the given path as an Attachment. In general, the browser will prompt the user to download. As an example,:

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

Res.end ([data] [, encoding] method)

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

This method helps end the Response process. As an example,:

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

Res.format (object) method

 res . format ( object ) 

This method helps execute the Accept HTTP Header section format on the Request object when performing. As an 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 phan Request va phan hoi voi code 406 res . status ( 406 ). send ( 'Not Acceptable' ); } }); 

Res.get method (field)

 res . get ( field ) 

This method helps return a certain field in the HTTP Response Header. As an example,:

 res . get ( 'Content-Type' ); 

Res.json method ([body])

 res . json ([ body ]) 

This method helps send a response in JSON format. As an example,:

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

Res.jsonp method ([body])

 res.jsonp ([body]) 

This method helps send a response in JSON format with the help of JSONP. As an example,:

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

Res.location method (path)

 res.location (path) 

This method helps set the Location field of the HTTP Header based on the given path parameter. As an example,:

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

Res.redirect method ([status,] path)

 res.redirect ([status,] path) 

This method helps redirect to a URL from a specific path path, with a certain Status Code. As an example,:

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

Res.render (view [, locals] [, callback] methods)

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

This method helps render a view and send the rendered HTML string to the Client. As an example,:

 // Guess a view to client 
 res.render ('index'); 

 // Access a screen to view 
 res.render ('user', {name: 'Tobi'}, function (err, html) { 
 // . 
 }); 

Method res.send ([body])

 res.send ([body]) 

This method helps send HTTP Response. As an example,:

 res.send (new Buffer ('whoop')); 
 res.send ({some: 'json'}); 
 res.send (' 
 some html '); 

Res.sendFile method (path [, options] [, fn])

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

This method helps transmit the file at the given path address. Set Content-Type based on the filename extension. As an example,:

 res.sendFile (fileName, options, function (err) { 
 // . 
 }); 

Res.sendStatus method (statusCode)

 res.sendStatus (statusCode) 

This method helps set the Status Code and send its string representation as a body of Response. As an example,:

 res.sendStatus (200); // tuong duong phuong res.status (200) .send ('OK') 
 res.sendStatus (403); // tuong duong phuong res.status (403) .send ('Forbidden') 
 res.sendStatus (404); // tuong duong phuong res.status (404) .send ('Not Found') 
 res.sendStatus (500); // In the same way, res.status (500) .send ('Internal Server Error') 

Res.set method (field [, value])

 res.set (field [, value]) 

This method helps set the field field of the HTTP Header to the value value. As an example,:

 res.set ('Content-Type', 'text / plain'); 

 res.set ({ 
 'Content-Type': 'text / plain', 
 'Content-Length': '123', 
 'ETag': '12345' 
 }) 

Res.status (code)

 res.status (code) 

This method helps set HTTP Status for Response. As an example,:

 res.status (403) .end (); 
 res.status (400) .send ('Bad Request'); 
 res.status (404) .sendFile ('/ absolute / path / to / 404.png'); 

Res.type (type) method

 res.type (type) 

This method helps set the Content-Type of the HTTP Header to the MIME type type. As an example,:

 res.type ('. html'); // => 'text / html' 
 res.type ('html'); // => 'text / html' 
 res.type ('json'); // => 'application / json' 
 res.type ('application / json'); // => 'application / json' 
 res.type ('png'); // => image / png: 

According to Tutorialspoint

Previous post: Request object in Node.js

FAQ

What should you know about properties of the Response object in Node.js?

The following table lists some properties of the Response object in Node.js.

What should you know about method of Response object in Node.js?

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

What is Response Object in Node.js?

The object res represents the HTTP Response that the Express application sends when receiving an HTTP Request.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.