Pickle
Pickle

Reputation: 57

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 4

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.

Upvotes: 2

Views: 7125

Answers (2)

A. van Loon
A. van Loon

Reputation: 94

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.

Upvotes: 2

AbdulAziz
AbdulAziz

Reputation: 21

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.

Upvotes: 2

Related Questions