Monday 18 March 2019

Express: res.sendStatus(statusCode): Send response code


res.sendStatus(statusCode)
Sets the response HTTP status code to statusCode and send its string representation as the response body.

Example
res.sendStatus(200); // equivalent to res.status(200).send('OK')
res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')

index.js
'use strict'

const express = require('express')

const app = express()

app.get('/', (req, res) => {
 res.sendStatus(200)
})

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

Run index.js


Open the url ‘http://localhost:3000/’ in browser, you can see below message.


Previous                                                 Next                                                 Home

No comments:

Post a Comment