HTTP
response.statusCode
Sets the status used when response headers are sent implicitly. Set it before writing body bytes; a late assignment cannot revise what the client already received. Choose a status that reflects the application result. Tested on Node.js 22.16.0. Example output (one line per entry): ["404 missing"].
Formal Syntax & Structure
nodejs syntaxnodejs
response.statusCode = codeReturn Value
number
HTTP response status code.
Working Example
Example Snippetnodejs
import http from 'node:http';const server = http.createServer((req, res) => { res.statusCode = 404; res.end('missing'); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try { const r = await fetch(url); console.log(r.status, await r.text());}finally { server.closeAllConnections(); await new Promise(r => server.close(r));}