Express.js Response Object

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.

        // calling the express js
    var express = require('express');
    var app = express()

    // response object - default route
    app.get('/',(req,res)=>{
      res.send("welcome to express js")
    })

    //app.get(route,callback(req,res))
    // response object - with another name routerout
    app.get("/name", (req,res)=>{
      res.send("My Name is CGVA");
    })

    //json data routing
    app.get("/data",(req,res)=>{
      res.send([
        {
          name:"cgva",
          course:"reactjs"
        },
        {
          name:"cgva",
          course:"reactjs"
        }
      ])
    })

    // creating the server
    app.listen(8000,()=>{
      console.log("port is running ...")
    })