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

Express.js · Pipe

res.statusMessage

res.statusMessage is the HTTP reason phrase associated with an Express response’s status code. Express’s error handler derives it from the final status code, while application code may assign a custom phrase.

How it works

res.statusMessage is the response property used for the HTTP reason phrase associated with the status code. Express’s error handling sets it according to the response status code; applications can also assign a custom value before ending the response.

Syntax

res.statusMessage

Key points

  • During Express error handling, the reason phrase is selected from the resulting HTTP status code.
  • An error’s status or statusCode influences the status code that Express uses before determining the status message.
  • Invalid error status values are replaced with 500, so the corresponding message is based on the normalized status.

Common mistakes

  • When Express writes an error response, it derives the status message from the status code; assigning a custom value may replace that phrase.

Example: Set a response status message

const express = require('express');
const app = express();

app.get('/error', (req, res) => {
  res.statusCode = 418;
  res.statusMessage = 'Teapot';
  res.end();
});

app.listen(3000);

Sets a custom reason phrase alongside the response status code.

When to use it

Provide a custom HTTP reason phrase

Custom protocol responses

Reflect the normalized error status

Error middleware

Set response metadata before ending

Low-level response handling

Interview questions

QWhat is res.statusMessage used for in Express?

It is the HTTP reason phrase sent with the response status line, such as Not Found for status 404. Express uses it during error handling based on the status code.

Follow-up: What happens if an error provides an invalid status? Express normalizes status values outside the 4xx or 5xx ranges to 500, then derives the message from that status.

QDoes res.statusMessage determine the response body?

Express sets res.statusMessage according to the status code when an error response is written. It is separate from the response body, which Express generates according to the environment.

Follow-up: How can an application control the body independently? Set the status and message properties, then explicitly send or end the response with the desired body.

QHow does error handling relate res.statusMessage to the response body?

No. The error handler sets the status message from the status code, while the body is the status-code HTML in production or the error stack otherwise. The two response components are handled separately.

Follow-up: What other error information can be added? Headers supplied through an err.headers object are also added to the response.

  • res.status
  • res.statusCode
  • res.sendStatus
  • err.status
  • err.statusCode

Source: Error Handling · Express.js 4.x. Checked 2026-10-15.