我正在使用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"
}

它不应该抛出错误消息,因为这些分数是正输入.当输入也是正分数时,我可以得到一些关于如何防止错误消息的指导吗?

推荐答案

当您在条件中判断输入"%1/2"时,它是字符串,因此需要将其转换为数字

如果您直接try 使用Number("1/2")方法将分数字符串转换为数字,则会抛出NaN.所以使用eval("1/2")

它会解决你的问题

if (!(eval(req.body.BottomMat) > -1)) {
        res.status(422).json({ messages: "Please insert a positive value" });
    }

如果您需要安全的方法,可以添加正则表达式来验证输入的数字是否有效

if (!(req.body.BottomMat.match(/^[0-9\/\.]+$/) != null && eval(req.body.BottomMat) > -1)) {
  ...
}

因此,如果输入包含0-9 and / and .以外的任何文本,它将返回FALSE,这样就不会对恶意输入运行EVERA

Javascript相关问答推荐

可以将SuperTest导入为ES 6模块吗?

CSS背景过滤器忽略转换持续时间(ReactJS)

类型错误:tasks.map不是函数(MERN堆栈)

响应式JS DataTable中的Bootstrap 5弹出程序无法正常工作

在shiny 模块中实现JavaScript

我不明白这个react 组件有什么问题

foreach循环中的Typescript字符串索引

如何通过onClick为一组按钮分配功能;

如何避免移动设备中出现虚假调整大小事件?

使用useEffect,axios和useParams进行react测试

手机上的渲染错误文本必须在文本组件中渲染,但在浏览器上没有问题<><>

使用Promise.All并发解决时,每个promise 的线性时间增加?

如何将多维数组插入到另一个多维数组中?

WhatsApp Cloud API上载问题:由于MIME类型不正确而导致接收&Quot;INVALID_REQUEST";错误

对路由DOM嵌套路由作出react

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

如何在 Select 文本时停止Click事件?

Phaserjs-创建带有层纹理的精灵层以自定义外观

未捕获的运行时错误:调度程序为空

使用createBrowserRoutVS BrowserRouter的Reaction路由