req.is()
Checks whether the request Content-Type matches the given MIME type.
Syntax
req.is(type) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
type | string | Yes | A content type or shorthand, e.g. `'json'`, `'html'`, `'application/*'`. |
Returns
string | false | null — The matched type, false if no match, null if no body.
Examples
app.post('/upload', (req, res) => {
if (req.is('application/json')) return res.send('json body');
res.status(415).send('unsupported');
});
Output
$ curl -X POST -H 'Content-Type: application/json' -d '{}' localhost:3000/upload
json body
app.post('/x', (req, res) => {
res.send(String(req.is('text/*')));
});
Output
$ curl -X POST -H 'Content-Type: text/plain' -d hi localhost:3000/x
text/plain
Notes
Tests the request `Content-Type`, not what the client accepts (use
`req.accepts` for that). Returns `null` when there is no request
body. Respond 415 Unsupported Media Type on a mismatch.