Why I'm getting ERR_HTTP_INVALID_STATUS_CODE?

Issue

I am making a simple calculator that does calculation on server as a beginner project. This is the code I have written in .js file:

const express = require("express");
const bodyParser=require("body-parser");

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

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 num3=num1+num2;
    res.send(num3);  // gives error if i don't write it as string >>like
                     //res.send("your answer is"+num3); >> this one doesn't throw an error
})
app.listen(3001, function () {
    console.log("server started at port 3001");
});

Why is this so? I am a beginner so please explain simply.

Solution

This problem is happening because you are giving send a parameter which is a Number, that isn’t supported by the method. Here is what the doc says:

res.send([body])

The body parameter can be a Buffer object, a String, an Object, Boolean, or an Array.

Answered By – yousoumar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published