Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Pipe

res.statusCode

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).

How it works

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.

Syntax

res.statusCode = statusCode;

Parameters

NameTypeDescription
statusCodenumberThe numeric HTTP status code to assign to the response.

Returns

No separate return value; this is a response property assignment. The related res.status(code) method returns the Response object.

Key points

  • The property belongs to the response object available in route handlers and middleware.
  • Express exposes this Node response property because its response object extends Node’s native response.
  • Assigning a number selects the status code that will accompany the response.
  • For Express-style code, res.status(code) provides a chainable way to perform the same status selection.

Common mistakes

  • Use a valid HTTP status code; some Node.js versions throw for values outside the permitted range.

Example: Set a response status directly

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.

When to use it

Set a custom status with JSON or text

A handler must return a precise protocol status together with a response body.

Prepare status in middleware

Middleware determines the outcome before a later handler sends the response.

Use the chainable Express equivalent

Application code prefers fluent response construction.

Interview questions

QWhat does 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.

QWhat values are safe to assign?

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.

QHow does 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()

Source: Response - Express.js 4.x. Checked 2026-10-15.