使用NodeJS/Express为REST API计算公式时,如何限制输入负数



我正在使用NodeJS为图片框架计算器创建一个REST API。用户输入适当的POST请求,例如:

{
"FrameWidth": "16",
"FrameWidthFraction": "0",
"FrameHeight": "20",
"FrameHeightFraction": "0",
"PictureWidth": "11",
"PictureWidthFraction": "0",
"PictureHeight": "17",
"PictureHeightFraction": "0",
"MiddleMat": "1",
"MiddleMatFraction": "0",
"BottomMat": "0",
"BottomMatFraction": "1/4"
}

JSON输出如下:

{
"messages": [
{
"text": "Place the parallel mat guide at the following inch mark, then make the respective width and height cut starting with the Top Mat, Middle Mat, then Bottom Mat:"
},
{
"text": "Top Mat Width Cut = 1/2 inches"
},
{
"text": "Top Mat Height Cut = 1 1/2 inches"
},
{
"text": "Middle Mat Width Cut = 1 1/2 inches"
},
{
"text": "Middle Mat Height Cut = 2 1/2 inches"
},
{
"text": "Bottom Mat Width Cut = 1 1/2 inches"
},
{
"text": "Bottom Mat Height Cut = 2 1/2 inches"
},
{
"buttons": [
{
"title": "Go to I Was Framed!",
"type": "web_url",
"url": "https://iwasframed.com/"
}
]
}
]
}

我用来处理输入、计算方程和显示输出的JavaScript如下:

app.post("/api/chatfuel/calculator/triplematapi", (req,res) => {

let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let FrameHeight = 1 * req.body.FrameHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let PictureWidth = 1 * req.body.PictureWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let PictureHeight = 1 * req.body.PictureHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let MiddleMat = 1 * req.body.MiddleMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let BottomMat = 1 * req.body.BottomMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let TopMatWidth = ((FrameHeight)-(PictureHeight))/2-(MiddleMat);
let TopMatHeight = ((FrameWidth)-(PictureWidth))/2-(MiddleMat);
let MiddleMatWidth = ((FrameHeight)-(PictureHeight))/2;
let MiddleMatHeight = ((FrameWidth)-(PictureWidth))/2;
let BottomMatWidth = ((FrameHeight)-(PictureHeight))/(2)+(BottomMat);
let BottomMatHeight = ((FrameWidth)-(PictureWidth))/(2)+(BottomMat);
res.json({"messages": [{"text": "Place the parallel mat guide at the following inch mark, then make the respective width and height cut starting with the Top Mat, Middle Mat, then Bottom Mat:"},{"text": `Top Mat Width Cut = ${new Fraction(TopMatWidth).toString()} inches`},{"text": `Top Mat Height Cut = ${new Fraction(TopMatHeight).toString()} inches`},{"text": `Middle Mat Width Cut = ${new Fraction(MiddleMatWidth).toString()} inches`},{"text": `Middle Mat Height Cut = ${new Fraction(MiddleMatHeight).toString()} inches`},{"text": `Bottom Mat Width Cut = ${new Fraction(BottomMatWidth).toString()} inches`},{"text": `Bottom Mat Height Cut = ${new Fraction(BottomMatHeight).toString()} inches`},{"buttons": [{"title": "Go to I Was Framed!","type": "web_url","url": "https://iwasframed.com/"}]}]});
});

我遇到的问题是,即使负数是无效的条目,用户也可以输入负数。

我试图查看这个网站上的信息:JavaScriptif…else语句和声明,并使用这个:

function testNum(a) {
let result;
if (a > 0) {
result = 'positive';
} else {
result = 'NOT positive';
}
return result;
}
console.log(testNum(-5));

我也从同一个网站上读到了可能的truthyfalsy条件,但我只是不确定我是否朝着正确的方向前进。我试图实现的是,我需要将用户输入限制为基于JSON输入求解的等式中的正数。

有人能为我提供一些指导吗?在使用Node JS/Express使用我的上述方程计算方程时,如何限制输入负数?

就后端而言,一个简单的解决方案是在输入值不是正数的情况下返回错误消息。例如

app.post("/api/chatfuel/calculator/triplematapi", (req,res) => {
// add this code before your calculations.
// do the same for the fields you need validation.
if (!(req.body.FrameWidth > 0)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
...
});

在前端,你可以做的是限制输入选项

<input type="number" name="FrameWidth" min="0">

如果这是你需要的,我建议你使用像快速验证器这样的验证库

最新更新