• RubyonRails
  • Java IO
  • Struts
  • jQuery
  • PHP
  • Servlet
  • Core Java
  • JavaScript
  • Spring

  • <

    ExpressJS Response

    The Response object (res) specifies the HTTP response which is sent by an Express app when it gets an HTTP request.

  • 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



    Properties Description
    res.set(name, value)Sets a response header. This is not something you will normally be doing manually.
    res.cookie(name, value, [options]),
    res.clearCookie(name, [options])Sets or clears cookies that will be stored on the client. This requires some middle‚?? ware support;
    res.format(object)This method allows you to send different content depending on the Accept request header.
    res.localsres.locals is an object containing default context for rendering views. res.
    res.links(links)Sets the Links response header. This is a specialized header that has little use in most applications.
    res.redirect([status], url)Redirects the browser. The default redirect code is 302 (Found).


    Response Methods



    Response Links method



    syntax
    res.links(links)   
    

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

    Example
    
    res.links({  
      next: 'http://api.rnd.com/users?page=5',  
      last: 'http://api.rnd.com/users?page=10'  
    });  
    
    


    Response Redirect method



    syntax
    res.redirect([status,] path)   
    

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

    Example
    res.redirect('http://javameant.com');  
    


    Response Set method



    syntax
    res.set(field [, value])   
    

    This method is used to set the response of HTTP header field to value.

    Example
    
    
    res.set('Content-Type', 'text/plain');  
      
    res.set({  
      'Content-Type': 'text/plain',  
      'Content-Length': '564',  
    })  
    


    Response Status method



    syntax
    res.status(code)   
    

    This method sets an HTTP status for the response.

    Example
    res.status(403).end();  
    res.status(400).send('Bad Request');  
    


    Response Type method



    syntax
    res.type(type)   
    

    This method sets the content-type HTTP header to the MIME type.

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


    Response Format method



    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');  
      }  
    });  
    
    
    


    Response Cookie method



    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.

    Example
    res.cookie('name', 'java', { domain: '.javameant.com', path: '/admin', secure: true });  
    res.cookie('Section', { Names: [java,meant] });  
    res.cookie('Cart', { items: [php,java,python] }, { maxAge: 300000 });  
    
    


    Response ClearCookie method



    syntax
    res.clearCookie(name [, options])   
    

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

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


    Response Download method



    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('/dipika.png');  
    
    
















    © copyright 2017-2021 Completedone pvt ltd.