Send a status with text or JSON
An endpoint must report a client or server outcome while returning a body.
Express.js · Function
res.status(code) sets an Express response’s HTTP status code and returns the response object for chaining. Use it before methods that send or finish the response.
Call res.status(code) to assign the HTTP status for the response. Because it returns the Response object, chain it with methods such as json(), send(), end(), or sendFile() to complete the response.
res.status(code)| Name | Type | Description |
|---|---|---|
code | Number | The numeric HTTP status code to set on the response. |
Response
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.status(200).json({ ok: true });
});
app.listen(3000);Run this Express app and request /health to receive a JSON response with HTTP status 200.
An endpoint must report a client or server outcome while returning a body.
A handler must finish without returning content.
A route returns a file but needs a non-default status.
res.status(code) sets the HTTP status code and returns the Response object. Its chainable return makes patterns such as res.status(400).send('Bad Request') possible.
Follow-up: How would you send only a status and its standard text? Use res.sendStatus(code), which sets the status and sends the registered status message.
The argument is code, a Number representing the HTTP status code for the response. The method is a chainable alias of Node’s response.statusCode.
Follow-up: What should follow res.status() when the request must be completed? Chain a method such as end(), send(), json(), or sendFile().
Use res.status() separately before sending a body, for example res.status(500).json({ error: 'message' }). This is the documented replacement for deprecated status arguments on older response-method signatures.
Follow-up: Why avoid res.json(body, status) in modern Express code? Its positional status form is deprecated; set the status separately and chain the response method.
res.send()res.json()res.end()res.sendFile()res.sendStatus()response.statusCode