Express res.sendStatus() Method
What you’ll learn
- How to send status-only responses quickly with
res.sendStatus(). - How it compares with
res.status().send(). - When shorthand status responses are appropriate.
- How to avoid double-response issues after sending status.
Syntax
javascript
res.sendStatus(statusCode)1
Simple health check response
javascript
app.get('/health', function (req, res) {
res.sendStatus(200);
});2
Send not found quickly
javascript
app.get('/reports/:id', function (req, res) {
var exists = false;
if (!exists) return res.sendStatus(404);
res.send('Report found');
});3
Send no-content status
javascript
app.delete('/cache', function (req, res) {
// delete cache...
res.sendStatus(204);
});⚠️ Common pitfalls
- Do not call another response method after
res.sendStatus(). - Use
res.status().send()if you need a custom response body. - Return after early status sends to keep handler flow clear.
❓ FAQ
It sets the status code and sends the default status text as the response body.
res.sendStatus() is a shorthand for common status-only responses with standard text.
No, it sends standard status text. For custom content use res.status(code).send(customMessage).
It is useful for lightweight endpoints like health checks or quick error responses.
No, sendStatus ends the response, so additional sends cause double-response issues.
Did you know?
res.sendStatus(code) sets the HTTP status and sends its standard text as the response body.
4 people found this page helpful
