RangeError ERR_HTTP_INVALID_STATUS_CODE Invalid status code

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code
  2. Specify a default status code if the variable is undefined
  3. If returning an integer, convert it to a string
  4. Make sure you don't have any syntactical errors

# RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code

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.

rangeerror err http invalid status code

Here is an example of how the error occurs.

index.js
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:

  • RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined

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.

index.js
res.status(0).send('<h2>bobbyhadz.com</h2>');

0 is not a valid status code, so the code sample causes the following error.

  • RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 0

If you are conditionally determining the status code with which you respond to the client, use an if/else statement.

index.js
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}`); });
The code for this article is available on GitHub

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.

# Specify a default status code if the variable is 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.

index.js
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}`); });
The code for this article is available on GitHub

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

index.js
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.

# If returning an integer, convert it to a string

The error is also raised if you return an invalid status code as an integer using res.send().

index.js
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 for this article is available on GitHub

The code sample above causes the following error.

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

index.js
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.

index.js
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}`); });
The code for this article is available on GitHub

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.

# Make sure you don't have any syntactical errors

The error is also raised if you have a syntactical error in the value you're returning with res.send().

index.js
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 for this article is available on GitHub

The code sample above causes the following error:

  • RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: { id: 1 }

The issue in the example is that we haven't wrapped the two objects in an array [].

Making the following change resolves the issue.

index.js
res.send([{id: 1}, {id: 2}]);

Now the res.send() method is called with an array of objects.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev