发送公式器之前的安全性问题-nodejs

问题描述 投票:0回答:1

我提出了一个随机数学问题来检查用户是否不是机器人,所以我想检查用户是否具有正确的数字。

let n1 = Math.floor(Math.random() * 100) + 2;
let n2 = Math.floor(Math.random() * 100) + 2;
let result = n1 + n2;

我把它放在router.get中。我有一个带配方设计师的router.post,我要检查

 if (req.body.SecureValue !== result) {
        // errorMessage
    }

但是如果我执行它会抛出

ReferenceError:结果未定义

我试图在发布请求内部以及每个路由器外部定义变量,但是它不起作用。

javascript node.js
1个回答
0
投票

如果在另一个方法中定义了此变量,则在将响应发送到客户端之后,所有变量将从内存中删除。您需要全局定义它,并在两个请求中发送它:

let n1 = Math.floor(Math.random() * 100) + 2;
let n2 = Math.floor(Math.random() * 100) + 2;
let result = n1 + n2;

route.get('/path', function(req, res) {
   //use n1 and n2 here
});

route.post('/otherpath', function(req, res) {
   //use results here
});

© www.soinside.com 2019 - 2024. All rights reserved.