我有一个示例代码:
app.get('/webhook', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === VALIDATION_TOKEN) {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});

然后现在我想将回调函数分解为新模块。我在Verify.js中保存此函数:
function ValidationToken(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === VALIDATION_TOKEN) {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
}
exports.ValidationToken=ValidationToken;

但现在我无法使用此功能进行回调:
var VefiryQuery=require("./Facebook/VerifyRequest");
var ValidationToken=VefiryQuery.ValidationToken();
app.get('/webhook', ValidationToken(req,res){});

然后我得到错误:没有定义req和res 我怎么能打破它?
答案 0 :(得分:3)
在将其作为回调传递之前,您不必调用ValidationToken()
函数:
var VefiryQuery = require("./Facebook/VerifyRequest");
var ValidationToken = VefiryQuery.ValidationToken;
app.get('/webhook', ValidationToken);