【发布时间】:2021-02-18 07:18:39
【问题描述】:
这是一个简单的bmi计算器和一个可以加数字的普通计算器。 html 表单正确加载到浏览器,但是一旦我提交表单,我在 /bmicalculator 路由上就收到了 500 错误代码。 正常的计算器路线工作正常(“/”)。
错误代码:
RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码: 0.0027180899908172637 在 ServerResponse.writeHead (_http_server.js:255:11) 在 ServerResponse._implicitHeader (http_server.js:246:8) 写时 (_http_outgoing.js:685:9) 在 ServerResponse.end (_http_outgoing.js:799:5) 在 ServerResponse.send (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\response.js:221:10) 在 G:\WebDevelopmentFolder\Calculator\calculator.js:36:7 在 Layer.handle [as handle_request] (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\layer.js:95:5) 在下一个(G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\layer.js:95:5)
bmicalculator.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>BMI Calculator</title>
</head>
<body>
<h1>BMI Calculator</h1>
<form action="/bmicalculator" method="post">
<input type="text" name="weight" placeholder="weight">
<input type="text" name="height" placeholder="height">
<button type="submit">Calculate BMI</button>
</form>
</body>
</html>
calculator.js:
//jshint esversion:6
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
//sending response
app.get("/",function(req, res){
res.sendFile(__dirname + "/index.html");
});
//post
app.post("/",function(req,res){
var num1 = Number(req.body.num1);
var num2 = Number(req.body.num2);
var result = num1 + num2;
res.send("The result is: "+result);
});
//get
app.get("/bmicalculator",function(req, res){
res.sendFile(__dirname + "/bmicalculator.html");
});
//post
app.post("/bmicalculator",function(req, res){
var weight = parseFloat(req.body.weight);
var height = parseFloat(req.body.height);
var bmi = weight/(height * height);
res.send("Your BMI is: ", bmi);
});
//listen to port 3000
app.listen(3000, function(){
console.log("Server listening on port 3000");
});
【问题讨论】:
标签: javascript node.js express