Set a custom status with JSON or text
A handler must return a precise protocol status together with a response body.
Express.js · Pipe
res.statusCode sets the HTTP status code on an Express response. Assign it before sending or ending the response, or use chainable res.status(code).
res.statusCode is the Node.js response property exposed by Express. Assign a valid HTTP status code before sending the response; Express also provides res.status(code) as a chainable alternative.
res.statusCode = statusCode;| Name | Type | Description |
|---|---|---|
statusCode | number | The numeric HTTP status code to assign to the response. |
No separate return value; this is a response property assignment. The related res.status(code) method returns the Response object.
res.status(code) provides a chainable way to perform the same status selection.const express = require('express');
const app = express();
app.get('/created', (req, res) => {
res.statusCode = 201;
res.json({ok: true});
});
app.listen(3000);This route assigns the response status directly, then sends a JSON body.
A handler must return a precise protocol status together with a response body.
Middleware determines the outcome before a later handler sends the response.
Application code prefers fluent response construction.
res.statusCode control?res.statusCode is the response property used to select the HTTP status code. Set it before a response-ending operation such as res.json() or res.end().
Follow-up: How can the same status be set in Express style? Use res.status(code), which is chainable.
The documented valid range is 100 through 599. Values outside that range may cause some Node.js versions to throw.
Follow-up: What should an application do when status input is user-controlled? Validate or map it to an allowed status before assignment.
res.status() relate to res.statusCode?res.status(code) sets the HTTP status and returns the response, allowing calls such as res.status(400).send(...). It is documented as a chainable alias of Node’s response.statusCode.
Follow-up: When only a status and its standard text are needed, use res.sendStatus(code) instead.
res.status()res.sendStatus()res.send()res.json()res.end()