Provide a custom HTTP reason phrase
Custom protocol responses
Express.js · Pipe
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.
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.
res.statusMessagestatus or statusCode influences the status code that Express uses before determining the 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.
Custom protocol responses
Error middleware
Low-level response handling
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.
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.
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.statusres.statusCoderes.sendStatuserr.statuserr.statusCode