我正在开发一个 node.js/express 应用程序来检查用户帐户的详细信息,以确定他们是否符合登录资源的条件。最初,所有代码都在一个 app.js 文件中,但现在我试图将它分解成模块,以便它可以用于多个位置和资源,每个位置和资源都有自己的特定标准。该应用程序通过在查询字符串中接收用户名和密码来工作,我将其解析为各自的部分。然后将用户名和密码格式化为 API 调用以检查凭据是否有效。如果它们有效,则脚本会从他们的帐户中检索更多信息,并根据获取资源访问权限的要求检查返回值。
我已经设法设置目录和路由,将请求正确定向到正确的脚本。我在将我的 API 调用转换为可在多个脚本中使用的实用程序时遇到问题(进行调用不需要更改实用程序,但每个脚本都有不同的标准来比较返回值)。
我遇到了两个问题。首先,我有多个需要同步运行的函数,它们在之前的调用完成之前被调用,所以它们进行调用所需的值在它们开始时仍然是未定义的。其次,我不太明白如何将实用程序的返回值传递回登录脚本。
登录模块的内容如下:
var request = require('request');
const pAuth = require('../../utils/authPatron');
var username = '';
var password = '';
var bNumber ='';
var resource = {
authenticate: (req, res, next) => {
username = req.query.username;
password = req.query.password;
console.log('Starting authentication');
console.log(username, password);
if (username.length < 9 || password.length != 4) {
sendFalse(req, res);
}
else {
console.log('Calling pAuth');
return pAuth(username, password, function(bNumber) {
console.log(bNumber)
next();
});
}
},
criteriaCheck1: (req, res, next) => {
console.log(bNumber);
console.log('Starting criteriaCheck1 process.');
next();
},
criteriaCheck2: (req, res, next) => {
console.log('Starting criteriaCheck2 process.');
next();
}
};
Authenticate 解析用户名和密码并将它们保存为整个脚本可以访问的变量(有更简单的方法吗?),然后调用实用程序 pAuth 并传递用户名和密码。 pAuth 实用程序具有以下代码:
var request = require('request');
module.exports = (barcode, pin, bNumber) => {
console.log('Running authenticate utility');
var data = JSON.stringify({'barcode': barcode, 'password': password});
var result = null;
request({
headers: {defines customer headers},
url: 'urlToRetrievePatronInfo.somedomain',
body: data,
method: 'POST',
}, function (err, resp, body) {
var resBody = JSON.parse(body);
if (resp.statusCode == 200) {
console.log('Successfully authenicated');
var bNumber = resBody.patronKey
console.log(bNumber);
return bNumber;
}
});
}
Authenticate 正确地将查询值传递给 pAuth。 pAuth 正在检索 bNumber,但出于某种原因,我无法将其传回身份验证。此外,在进行身份验证之前会调用 criteriaCheck。我试图为 pAuth 创建一个回调函数并移动 next();进入回调函数,因此在身份验证完成之前不会调用它,但永远不会调用回调函数并且请求在两者之间挂起。
那么,如何防止在身份验证完成之前调用其他 criteriaCheck 函数?另外,如何将 pAuth 的结果传递到 criteriaCheck?
在此先感谢您的帮助!
最佳答案
你的代码很难阅读,但这里有些东西没有加起来:
console.log('Calling pAuth');
return pAuth(username, password, function(bNumber) {
console.log(bNumber)
next();
});
在这里:
if (resp.statusCode == 200) {
console.log('Successfully authenicated');
var bNumber = resBody.patronKey
console.log(bNumber);
return bNumber;
}
你可能应该做这样的事情
if (resp.statusCode == 200) {
console.log('Successfully authenicated');
bNumber(resBody.patronKey);
}
您的代码还有其他问题。 您对 pAuth 的调用是异步的,因此理想情况下您应该这样:
console.log('Calling pAuth');
return pAuth(username, password, function(err, result) {
if(err) return next(err);
console.log(result);
next();
});
你的 pAuth 应该是这样的:
var request = require('request');
module.exports = (barcode, pin, callback) => {
console.log('Running authenticate utility');
var data = JSON.stringify({'barcode': barcode, 'password': password});
var result = null;
request({
headers: {defines customer headers},
url: 'urlToRetrievePatronInfo.somedomain',
body: data,
method: 'POST',
}, function (err, resp, body) {
if (err) return callback(err);
if (resp.statusCode == 200) {
var resBody = JSON.parse(body);
console.log('Successfully authenicated');
return callback(null, resBody.patronKey);
} else {
return callback(new Error('Invalid response: ' + resp.statusCode);
}
});
}
您还会注意到我让回调函数接受两个参数:err、result。这是典型的 nodejs 约定,它允许异步函数处理错误和成功。如果 err 被定义,如果它没有被定义,就会发生错误,它起作用了,你应该能够读取结果。
关于javascript - 使用 express 在模块化的 node.js 应用程序中创建同步调用和传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565599/