Express res.status() Method
What you’ll learn
- How to set HTTP status codes with
res.status(). - How to chain status with send/json/end.
- How to use common status codes in real handlers.
- How to avoid incomplete responses after setting status.
Syntax
javascript
res.status(code)1
Return created status with JSON
javascript
app.post('/users', function (req, res) {
var user = { id: 101, name: req.body.name };
res.status(201).json(user);
});2
Return not found status with message
javascript
app.get('/users/:id', function (req, res) {
var found = false;
if (!found) return res.status(404).send('User not found');
res.send('User found');
});3
Use status with empty response
javascript
app.delete('/sessions/current', function (req, res) {
// session deleted...
res.status(204).end();
});⚠️ Common pitfalls
res.status()alone does not finish the response.- Always chain/send/end after setting status.
- Return after early error responses to avoid duplicate sends.
❓ FAQ
It sets the HTTP response status code for the outgoing response.
No. It only sets status; you still need send/json/end/redirect to complete response.
Yes, common usage is res.status(201).json(data) or res.status(404).send('Not found').
Use 201 Created when a new resource is successfully created.
Setting status but forgetting to send a response body or end the response.
Did you know?
res.status(code) sets the HTTP status and is usually chained with send, json, or end.
4 people found this page helpful
