Friday, 8 March 2019

Express: Exploring response object


‘response’ object is used to send the response to the client.

Example
response.send('Hello World')

index.js
'use strict'

const express = require('express')

const app = express()

app.get('/', (req, res) => {
 res.send('Hello World')
})

app.get('/welcome', (req, res) => {
 res.send('Dear Customer, Very Good Morning!!!!!')
})

const port = 3000
app.listen(port, () => console.log(`Application started listening on port ${port}!`));

Run index.js


Open the url 'http://localhost:3000/', you will see below message.



Open the url ‘http://localhost:3000/welcome’, you will see below message.
res.app: get reference to express application instance
res.headersSent: Check whether response headers send to the client or not
res.locals: Set response local variables
res.append(field [, value]): Append response header
res.attachment([filename]): Set Content-Disposition header
res.cookie(name, value [, options]): Set cookie name to value
res.clearCookie(name [, options]): Clears the cookie with this name
res.download(path [, filename] [, options] [, fn]): Download the file at given path
res.end([data] [, encoding]): End the response process
res.format(object): perform content negotiation on Accept http header
res.set(field [, value]): Set http response header field
res.get(field): Get http response header field
res.json([body]): Send json response
res.links(links): Populate response Link http header field
res.location(path): Set location header
res.redirect([status,] path): Redirect the url
res.render(view [, locals] [, callback])
res.send([body]): Send http response
res.sendFile(path [, options] [, fn]): Send file at given path
res.sendStatus(statusCode): Send response code
res.status(code): Set the status code for the response
res.type(type): Set Content-Type header

Previous                                                 Next                                                 Home

No comments:

Post a Comment