[英]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.尝试运行我的 web 应用程序时出现此错误,该应用程序应该是一个简单的计算器。 I'm using Nodejs with the express.js framework.
我将 Nodejs 与 express.js 框架一起使用。
this is my main js file:这是我的主要 js 文件:
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这是我的索引。html 代码
<!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.我试过用谷歌搜索答案,但我得到的只是错误代码:0。
edit: I should note that i am fairly new to nodejs and express.js.编辑:我应该注意到我对 nodejs 和 express.js 相当陌生。
It would be better to make json out of your response, that would make it more extendable in the future, something like:最好将 json 排除在您的响应之外,这将使其在未来更具可扩展性,例如:
{value: result} Then sending it would work cause the json will be parsed as a string. {value: result} 然后发送它会起作用,因为 json 将被解析为字符串。 According to the express documentation you can't send a number in the res.send method as tkausl pointed out.
根据快速文档,您不能像 tkausl 指出的那样在 res.send 方法中发送数字。
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.当我这样做时,错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.