最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 4 - Stack Overflow

programmeradmin1浏览0评论

I am getting this error when attempting to run my web application which is suppose to be a simple calculator. I'm using Nodejs with the express.js framework.

this is my main js file:

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

app.use(express.urlencoded({ extended : true}));

app.use(express.json());

app.get("/", function(req, res){
 res.sendFile(__dirname + "/index.html");
 });

app.post("/", function(req, res){

    var num1 = Number(req.body.num1);

    var num2 = Number(req.body.num2);

    var result = Number(num1 + num2);

    res.send(result);

 });

app.listen(port, function(){
     console.log("SERVER STARTED ON PORT " + port);
});

this is my index.html code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Calculator</title>
</head>
<body>
    <h1>Calculator</h1>
    <form action="/" method="post">
        <input type="text" name="num1" placeholder="first number">
        <input type="text" name="num2" placeholder="second number">
        <button type="submit" name="submit">Calculate</button>
    </form>

</body>
</html>

and this is the error code I get in terminal:

express deprecated res.send(status): Use res.sendStatus(status) instead calculator.js:23:9
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 4
    at ServerResponse.writeHead (_http_server.js:255:11)
    at ServerResponse._implicitHeader (_http_server.js:246:8)
    at ServerResponse.end (_http_outgoing.js:811:10)
    at ServerResponse.send (/Users/<name>/Desktop/Calculator/node_modules/express/lib/response.js:221:10)
    at /Users/<>/Desktop/Calculator/calculator.js:23:9
    at Layer.handle [as handle_request] (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/layer.js:95:5)
    at /Users/<name>/Desktop/Calculator/node_modules/express/lib/router/index.js:281:22

that is all the info I can get/give.

I have tried googling for answers but all I got was for error code: 0.

edit: I should note that i am fairly new to nodejs and express.js.

I am getting this error when attempting to run my web application which is suppose to be a simple calculator. I'm using Nodejs with the express.js framework.

this is my main js file:

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

app.use(express.urlencoded({ extended : true}));

app.use(express.json());

app.get("/", function(req, res){
 res.sendFile(__dirname + "/index.html");
 });

app.post("/", function(req, res){

    var num1 = Number(req.body.num1);

    var num2 = Number(req.body.num2);

    var result = Number(num1 + num2);

    res.send(result);

 });

app.listen(port, function(){
     console.log("SERVER STARTED ON PORT " + port);
});

this is my index.html code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Calculator</title>
</head>
<body>
    <h1>Calculator</h1>
    <form action="/" method="post">
        <input type="text" name="num1" placeholder="first number">
        <input type="text" name="num2" placeholder="second number">
        <button type="submit" name="submit">Calculate</button>
    </form>

</body>
</html>

and this is the error code I get in terminal:

express deprecated res.send(status): Use res.sendStatus(status) instead calculator.js:23:9
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 4
    at ServerResponse.writeHead (_http_server.js:255:11)
    at ServerResponse._implicitHeader (_http_server.js:246:8)
    at ServerResponse.end (_http_outgoing.js:811:10)
    at ServerResponse.send (/Users/<name>/Desktop/Calculator/node_modules/express/lib/response.js:221:10)
    at /Users/<>/Desktop/Calculator/calculator.js:23:9
    at Layer.handle [as handle_request] (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/<name>/Desktop/Calculator/node_modules/express/lib/router/layer.js:95:5)
    at /Users/<name>/Desktop/Calculator/node_modules/express/lib/router/index.js:281:22

that is all the info I can get/give.

I have tried googling for answers but all I got was for error code: 0.

edit: I should note that i am fairly new to nodejs and express.js.

Share Improve this question edited Apr 18, 2021 at 20:11 Pickle asked Apr 18, 2021 at 19:57 PicklePickle 572 silver badges7 bronze badges 3
  • 1 res.send([body]): The body parameter can be a Buffer object, a String, an object, Boolean, or an Array. You're passing it a number. – tkausl Commented Apr 18, 2021 at 20:01
  • 1 thank you, I fixed it by doing res.send([result]); instead of res.send(result); , though I get [<result>]. – Pickle Commented Apr 18, 2021 at 20:07
  • Also for anyone else who might be confused, note the difference between res.send(..) (which can't accept an integer) and res.status(...) (which can accept an integer) – Nate Anderson Commented May 22, 2024 at 15:56
Add a ment  | 

2 Answers 2

Reset to default 2

It would be better to respond with JSON, that would make it more extendable in the future, something like:

res.send({value: result})

Responding with an object would work, res.send will convert to your object to a JSON string and send it the response.

According to the express documentation you can't send a number in the res.send(...) method as @tkausl pointed out.

I had the same problem. Just add some text to your result number. It will work.

For example:

res.send("This is your answer " + result); 

When I did like this, the error disappeared.

发布评论

评论列表(0)

  1. 暂无评论