我正在使用Node JS&;Express构建一个图片边框计算器API.我正在使用的JavaScript是:
app.post("/api/chatfuel/calculator/triplematapi", (req,res) => {
if (!(req.body.FrameWidth > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.FrameHeight > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let FrameHeight = 1 * req.body.FrameHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.PictureWidth > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let PictureWidth = 1 * req.body.PictureWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.FrameHeight > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let PictureHeight = 1 * req.body.PictureHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.MiddleMat > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let MiddleMat = 1 * req.body.MiddleMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.BottomMat > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
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/"}]}]});
});
因此,POST请求的示例如下:
{
"FrameWidth": "16",
"FrameHeight": "20",
"PictureWidth": "11",
"PictureHeight": "17",
"MiddleMat": "1",
"BottomMat": "1/2"
}
我遇到的问题是,在添加判断以验证输入值是否为正之前,返回响应将进行计算.但是,通过添加此代码(我必须使用-1
,因为0
是有效的正输入):
if (!(req.body.BottomMat > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
然后输入任何fraction
,例如1/2
-它会抛出错误:
{
"messages": "Please insert a positive value"
}
它不应该抛出错误消息,因为这些分数是正输入.当输入也是正分数时,我可以得到一些关于如何防止错误消息的指导吗?