【问题标题】:getting a 500 error code on post request at a different route在不同的路线上发布请求时收到 500 错误代码
【发布时间】: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


    【解决方案1】:

    这样做res.send("Your BMI is: "+ bmi);,而不是像res.send("Your BMI is: ", bmi);那样使用,

    因为send()的第一个参数是状态码,第二个参数是内容。

    .send(200, "Your BMI is: "+ bmi)
    

    【讨论】:

    • 是的,谢谢!实际上将 , 更改为 + 有效。并且不需要第一个参数状态码
    【解决方案2】:

    请将此改写为res.send(`Your BMI is: ${bmi}`)

    代码错误 res.send的第二个参数是状态码,状态码不能是浮点数

    【讨论】:

    • 当我改为 res.send(Your BMI is:" +bmi) 时它确实有效
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 2020-02-19
    • 1970-01-01
    • 2021-02-12
    • 2016-03-20
    • 2012-12-19
    相关资源
    最近更新 更多