EN
Node.js - how to use res.send() with res.sendStatus()?
1 answers
0 points
How can I use res.send()
method with res.sendStatus()
at once to send status code and object with message at once?
1 answer
0 points
You can chain it together using one method after the other with dot (.
) separator.
Quick solution:
xxxxxxxxxx
1
res.status(200).send({ message: 'Message text...' });
Practical example
This example presents HTTP GET
request that returns all users
from a database with status 200
and object with some message
at once.
xxxxxxxxxx
1
app.get('/users', (req, res) => {
2
db.query('select * from users', (err, result) => {
3
res.status(200).send({ message: 'Message text...' });
4
});
5
});
0 commentsShow commentsAdd comment