Last updated: Apr 5, 2024
Reading timeยท4 min
undefined
The error "RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code" is
caused when you call the res.status()
method with an invalid status code.
To solve the error, make sure to call the method with a valid status code, e.g. an integer from 100 up to 600.
Here is an example of how the error occurs.
import express from 'express'; // ๐๏ธ Uncomment this if you use CommonJS require() syntax // const express = require('express'); const app = express(); app.get('/', (req, res) => { res.status().send('<h2>bobbyhadz.com</h2>'); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
If I make an HTTP request to http://localhost:5000
, the following error is
raised:
Notice that the res.status()
method is called without an integer value for the
status code.
Here is an example of calling the method with an invalid status code, e.g. 0
.
res.status(0).send('<h2>bobbyhadz.com</h2>');
0
is not a valid status code, so the code sample causes the following error.
If you are conditionally determining the status code with which you respond to
the client, use an if/else
statement.
import express from 'express'; // ๐๏ธ Uncomment this if you use CommonJS require() syntax // const express = require('express'); const app = express(); app.get('/', (req, res) => { const statusCode = 200; if (statusCode >= 100 && statusCode < 600) { res.status(statusCode).send('<h2>bobbyhadz.com</h2>'); } else { res.status(500); } }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
If the value of the statusCode
variable is in the range of valid status codes
(100 to 600), then we pass the variable to the res.status()
method, otherwise,
we call the method with the status code 500 (Server error).
You can view a list of all valid status codes on this wikipedia page.
The res.status()
method takes a valid status code as a parameter.
undefined
If you got the error "RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid
status code: undefined", use the
logical OR (||) operator to provide a
fallback status code if the variable is undefined
.
import express from 'express'; // ๐๏ธ Uncomment this if you use CommonJS require() syntax // const express = require('express'); const app = express(); app.get('/', (req, res) => { const statusCode = undefined; res.status(statusCode || 200).send('<h2>bobbyhadz.com</h2>'); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
If the statusCode
variable stores a falsy value, we fall back to a value
of 200.
The logical OR (||) operator returns the value to the right if the value to the
left is falsy (e.g. undefined
).
const statusCode = undefined; const newStatusCode = statusCode || 200; console.log(newStatusCode); // ๐๏ธ 200
You could use any other value that suits your use case, e.g. 500
.
The error is also raised if you return an invalid status code as an integer
using res.send()
.
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.send(0); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code sample above causes the following error.
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 0
The issue is that Express assumes that the integer is a status code and 0
is
an invalid status code.
You can resolve the issue by converting the number to a string.
import express from 'express'; // ๐๏ธ Uncomment this if you use CommonJS require() syntax // const express = require('express'); const app = express(); app.get('/', (req, res) => { const num = 0; res.send(String(num)); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
Simply pass the number to the String()
constructor and everything works as
expected.
Express no longer assumes that you're trying to send back a status code response.
An alternative approach would be to return an object.
import express from 'express'; // ๐๏ธ Uncomment this if you use CommonJS require() syntax // const express = require('express'); const app = express(); app.get('/', (req, res) => { const num = 0; res.json({id: num}); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
We passed an object to the res.json()
method.
The id
property of the object has a value of 0
, so the route handler returns
the following object - {id: 0}
.
You could change the name of the property to something that suits your use case.
The error is also raised if you have a syntactical error in the value you're
returning with res.send()
.
import express from 'express'; // ๐๏ธ Uncomment this if you use CommonJS require() syntax // const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send({id: 1}, {id: 2}); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code sample above causes the following error:
The issue in the example is that we haven't wrapped the two objects in an array
[]
.
Making the following change resolves the issue.
res.send([{id: 1}, {id: 2}]);
Now the res.send()
method is called with an array of objects.
You can learn more about the related topics by checking out the following tutorials: