我有一個專案,我在其中使用 Express.js 和 activedirectory 包進行 LDAP 身份驗證。
我有一個名為 ad.js 的檔案,其中包括 -
exports.authUser = (username, password) => {
ad.authenticate(username, password, function (err, auth) {
if (err) {
console.log('ERROR: ' JSON.stringify(err));
return;
}
if (auth) {
console.log('Authenticated!');
} else {
console.log('Authentication failed!');
}
})
}
我呼叫authUserapp.js-
app.post("/login", (req, res) => {
let username = req.body.username;
let password = req.body.password;
authUser(username, password);
})
身份驗證有效,但我想訪問authapp.js 中的值。執行此操作的正確方法是什么?
感謝您的任何建議。
uj5u.com熱心網友回復:
您可以“承諾”您的功能,然后await為結果。主函式回傳一個 Promise 物件,并且路由等待該 Promise 解決。
exports.authUser = (username, password) => {
return new Promise((resolve, reject) => {
ad.authenticate(username, password, (err, auth) => {
if (err) {
console.log(`ERROR: ${JSON.stringify(err)}`);
return reject(err);
}
console.log(auth ? 'Authenticated!' : 'Authentication failed!');
return resolve(auth);
});
});
};
app.post("/login", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
try {
const auth = await authUser(username, password);
} catch (error) {
// do something
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/526665.html
標籤:节点.js表示
上一篇:FetchAPI無法加載localhost:3000
下一篇:錯誤[ERR_HTTP_HEADERS_SENT]:發送到客戶端后無法設定標頭[nodeandexpressjs]--
