我提出了一个随机数学问题来检查用户是否不是机器人,所以我想检查用户是否具有正确的数字。
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
}
但是如果我执行它会抛出
我试图在发布请求内部以及每个路由器外部定义变量,但是它不起作用。
如果在另一个方法中定义了此变量,则在将响应发送到客户端之后,所有变量将从内存中删除。您需要全局定义它,并在两个请求中发送它:
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
});