Response object in Node.js

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. For 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 is used to send a File as an Attachment in the HTTP Response. For example:

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

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

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

This method is used to set the name of Cookie to value. The value parameter can be a string or an object converted to JSON. For 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 is used to delete specific cookies specified by the name parameter. For 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 is used to transfer the file in the given path as an Attachment. In general, the browser will prompt the user to download. For 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 is used to end the Response process. For example:

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

Res.format (object) method

 res . format ( object ) 

This method is used to execute the Accept HTTP Header section format on the Request object when performing. For 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 is used to return a certain field in the HTTP Response Header. For example:

 res . get ( 'Content-Type' ); 

Res.json method ([body])

 res . json ([ body ]) 

This method is used to send a response in JSON format. For example:

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

Res.jsonp method ([body])

 res.jsonp ([body]) 

This method is used to send a response in JSON format with the help of JSONP. For example:

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

Res.location method (path)

 res.location (path) 

This method is used to set the Location field of the HTTP Header based on the given path parameter. For 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 is used to redirect to a URL from a specific path path, with a certain Status Code. For 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 is used to render a view and send the rendered HTML string to the Client. For 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 is used to send HTTP Response. For 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 is used to transmit the file at the given path address. Set Content-Type based on the filename extension. For example:

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

Res.sendStatus method (statusCode)

 res.sendStatus (statusCode) 

This method is used to set the Status Code and send its string representation as a body of Response. For 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 is used to set the field field of the HTTP Header to the value value. For 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 is used to set HTTP Status for Response. For 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 is used to set the Content-Type of the HTTP Header to the MIME type type. For 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

4 ★ | 2 Vote

May be interested

  • How to Write Your First Program in JavaHow to Write Your First Program in Java
    java is an object-oriented programming language created in 1995 by james gosling, which means that it represents concepts as 'objects' with 'fields' (which are attributes that describe the object) and 'methods' (actions that the object can...
  • Class (class) and Object in C ++Class (class) and Object in C ++
    the main purpose of c ++ is to add object orientation to the c programming language and classes which are central features of c ++ that support object-oriented programming and are often called types user defined (user-defined).
  • The most dangerous object in the world, only 5 minutes of exposure can cause deathThe most dangerous object in the world, only 5 minutes of exposure can cause death
    this object is located in the chernobyl area, ukraine, as a result of one of the most horrifying radiation leaks in history. this object is shaped like a giant foot, so it is called the elephant's foot.
  • Document Object Model (DOM) in JavaScriptDocument Object Model (DOM) in JavaScript
    each site resides within a browser window that can be viewed as an object.
  • Objects in JavaScriptObjects in JavaScript
    javascript is an object oriented programming language (object oriented programming). a program language can be called object-oriented if it provides four basic capabilities to the programmer.
  • Object-oriented programming in PythonObject-oriented programming in Python
    python is a powerful object-oriented programming language. therefore, creating and using objects is very easy. this article will introduce some basic concepts in object-oriented programming, as well as how to create and use them.
  • Endpoint Detection and Response threats, an emerging security technologyEndpoint Detection and Response threats, an emerging security technology
    endpoint threat detection and response (etdr) is a term first introduced by security expert anton chuvakin from gartner in 2013 to refer to the tools mainly focus on detecting and investigating suspicious activities (as well as traces of other phenomena that don't always happen) on the server or endpoint.
  • Object projector and things to knowObject projector and things to know
    projectors are becoming increasingly popular and are widely used in the fields of life and work.
  • Object from unknown location continuously emitting radio wavesObject from unknown location continuously emitting radio waves
    the object sgr 0501+4516 is so strange that scientists believe it may hold the secret to the phenomenon of radio bursts.
  • Object-oriented programming in PHPObject-oriented programming in PHP
    we can imagine that the universe was made up of different objects like the sun, the moon, the earth, and so on. in the same way, you can imagine a car made of different objects like wheel, rudder, gear shift ... in such a way, concepts in object-oriented programming assume that everything is like an object and deploying a software by using different objects.