简体   繁体   English

为什么我收到 ERR_HTTP_INVALID_STATUS_CODE?

[英]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:这是我在.js文件中编写的代码:

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.发生此问题是因为您send了一个Number参数,该方法不支持该参数。 Here is what the doc says:这是文档所说的:

res.send([body]) res.send([正文])

The body parameter can be a Buffer object, a String , an Object , Boolean , or an Array . body参数可以是Buffer object、 StringObjectBooleanArray

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到 RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined - I am getting RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:4 - RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 4 RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效的状态代码:[object Object] 错误表达 4.16 - RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: [object Object] error express 4.16 ERR_HTTP_INVALID_STATUS_CODE — 登录网站失败时服务器关闭 - ERR_HTTP_INVALID_STATUS_CODE — server goes off when logging on the website fails 如何修复 RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:未定义我在尝试注册用户时收到此错误 - How to fix RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined i got this error when am trying to register user Node.js中的错误-UnhandledPromiseRejectionWarning:RangeError [ERR_HTTP_INVALID_STATUS_CODE] - Error in Node.js - UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE] 为什么我在jQuery中收到ERR_CERT_COMMON_NAME_INVALID错误? - Why i'm getting ERR_CERT_COMMON_NAME_INVALID error in jQuery? 为什么我在下面的 Luxon/Moment 代码中得到“Invalid DateTime”? - Why is that I'm getting "Invalid DateTime" in the following Luxon/Moment code? 为什么我没有收到 200 的状态码? - Why am I not getting a status code of 200? 我收到了(请求失败,状态码为 500) - I'm getting (Request failed with status code 500)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM