简体   繁体   中英

Why I'm getting ERR_HTTP_INVALID_STATUS_CODE?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM