Friday, 8 March 2019

Express: req.is(type): Check the matching content type


req.is(type)
It returns true, if type is matches to the incoming request’s "Content-Type" HTTP header field value. If the request has no body, returns null.

Example
req.is('html')

index.js
'use strict'

const express = require('express')

const app = express()

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

app.post('/users', (req, res) => {
 var isContHtml = req.is('html')
 
 res.send(`Is ContentType html : ${isContHtml}`) 
})

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

Run index.js

Open any rest client like Postman and hit below request.

Method: POST

Set below headers
Content-Type : text/html
Accept-Charset : utf-8

Body
Sample Data


Hit the request, you will receive below kind of response.



Previous                                                 Next                                                 Home

No comments:

Post a Comment