![](/img/trans.png)
[英]Cookies handling using javascript or Express.js or node.js?
[英]Can't run any normal javascript code in node.js express.js
我正在学习 express.js,我在这里有这个基本的 express 代码:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true }))
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html")
})
app.post("/", (req, res) => {
let n1 = req.body.num1 //num1 and num2 are coming from index.html which I have inciude above
let n2 = req.body.num2
let result = n1 + n2
res.send(result)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
但它需要 n1 和 n2 作为字符串。 所以如果我给 n1 = 2 和 n2 = 4 它返回 24 但不是 6 所以我尝试将 n1 和 n2 转换为数字所以我尝试过
let n1 = Number(req.body.num1)
let n2 = Number(req.body.num2)
但它给出了错误,称为:
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 5
at new NodeError (node:internal/errors:372:5)
at ServerResponse.writeHead (node:_http_server:275:11)
at ServerResponse._implicitHeader (node:_http_server:266:8)
at write_ (node:_http_outgoing:766:9)
at ServerResponse.end (node:_http_outgoing:855:5)
at ServerResponse.send (C:\Users\USERNAME \OneDrive\Desktop\lll\calculator\node_modules\express\lib\response.js:232:10)
at ServerResponse.sendStatus (C:\Users\USERNAME \OneDrive\Desktop\\calculator\node_modules\express\lib\response.js:375:15)
at C:\Users\USERNAME \OneDrive\Desktop\lll\calculator\calculator.js:18:9
at Layer.handle [as handle_request] (C:\Users\USERNAME \OneDrive\Desktop\lll\calculator\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\USERNAME \OneDrive\Desktop\lll\calculator\node_modules\express\lib\router\route.js:144:13)
即使我尝试控制台记录result
类型,它也会返回这种类型的错误。 有人能帮助我吗
index.html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Caluculator</title>
</head>
<body>
<h1>Caluculator</h1>
<form action="/" method="post">
<input type="text" name="num1" placeholder="Enter First Number" />
<input type="text" name="num2" placeholder="Enter Second Number" />
<button type="submit" name="submit">Calculate</button>
</form>
</body>
</html>
错误来自这一行 res.sendStatus(result) 结果不是有效的状态代码。 代替
let n1 = Number(req.body.num1);
放
const n1 = +req.body.num1;
const n2 = +req.body.num2;
const result= n1+n2;
res.status(200).send({result,
message:"success"});
状态码必须在 100 到 599 之间
该错误确实来自res.sendStatus(result)
而应该是res.send(result)
。 如果您确实想手动设置状态,那么它应该是res.status(200).send(result)
,将 200 交换为您要设置的状态码。
除此之外,您还应该将数字转换为字符串。 最高效的方法是有争议的,但我通常这样做的方法是${n1 + n2}
。
在您的情况下,通过上述更改,您的 JavScript 应如下所示:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true }))
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html")
})
app.post("/", (req, res) => {
let n1 = req.body.num1 //num1 and num2 are coming from index.html which I have inciude above
let n2 = req.body.num2
let result = n1 + n2
res.send(`${result}`)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Express 文档中有更多信息。
我找到了这样的答案Unable to send numbers using res.send() using express with node
您必须发送字符串而不是数字
app.post("/", (req, res) => { let n1 = Number(req.body.num1) //num1 and num2 are coming from index.html which I have inciude above let n2 = Number(req.body.num2) let result = n1 + n2 res.send(''+result) })
我测试并运行
let n1 = parseInt(req.body.num1);
let n2 = parseInt(req.body.num2);
let result = n1 + n2;
return res.status(200).send({result});
使用parseInt()
后,您将获得一个整数值。
此外,您可以在变量前使用+
号。
let num1 = "2"; let num2 = "4"; let n1 = parseInt(num1); let n2 = parseInt(num2); let result = n1 + n2; console.log('result >>>', result);
答案是 :
let n1 = req.body.num1
let n2 = req.body.num2
let result = Number(n1) + Number(n2)
res.send("" + result)
您只需添加一个空字符串。 我不知道为什么它有效,但它有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.