diff --git a/_includes/api/en/5x/req-accepts.md b/_includes/api/en/5x/req-accepts.md index 7258c71b4c..ea0f7f1362 100644 --- a/_includes/api/en/5x/req-accepts.md +++ b/_includes/api/en/5x/req-accepts.md @@ -33,4 +33,24 @@ req.accepts(['html', 'json']) // => "json" ``` +#### Example + +```js +app.get('/resource', function (req, res) { + // Check which response type the client prefers + const type = req.accepts(['json', 'html']) + + if (type === 'json') { + return res.json({ message: 'Hello world' }) + } + + if (type === 'html') { + return res.send('
Hello world
') + } + + // If none of the supported types are acceptable + res.status(406).send('Not Acceptable') +}) + + For more information, or if you have issues or concerns, see [accepts](https://github.com/expressjs/accepts).