Node.js POST 请求响应的异步处理
Async Processing for Node.js POST Request Response
我对 Node.js
比较陌生,我正在创建一个服务器,它将接受来自移动应用程序的 POST
请求,该应用程序的正文包含一个凭据,然后通过 GET
到另一台服务器。如果 GET
响应验证了凭据,则提取 UID 并调用 firebase admin SDK
以创建自定义令牌。下面是一段代码和两个被调用的函数 (a)
验证凭据并 (b)
生成自定义令牌。
//Listen for app to POST Credential
app.post('/', function(request, response) {
console.log('Request Body: ',request.body);
var Credential = request.body;
//Validate Credential
validateCredential(Credential)
//Get Authorization Token
getToken(userID)
//Return Token for POST Response
response.set('Content-Type','Text');
response.end(firebaseAuthToken);
});
//Create listener for POST function
app.listen(port, function() {
console.log('AuthServer is running and listening on port '+port);
});
//Function to Validate Credential
async function validateCredential(crdntl) {
//Call Service to validate Credential received
axios({
method: 'get',
url: 'https://.....',
})
.then(function(response) {
...check credential validation data
})
.catch(function (error) {
console.log('ERROR: Unable to Validate Credential');
//Unable to create Validate Credential so return error message for POST response
return ('ERROR1');
});
}
async function getToken(uid) {
admin.auth().createCustomToken(uid)
.then(function(customToken) {
var AuthToken = customToken;
var decoded = jwt.decode(AuthToken);
console.log('Decoded Token: '+'\n',decoded);
//Return Authorization Token for POST response
return (AuthToken);
})
.catch(function(error) {
console.log('ERROR: Unable to Create Custom Token', error);
//Unable to create Token so return error message for POST response
return ('ERROR2');
});
}
}
我需要返回 validateCredential 函数的结果并将其结果传递给 getToken 函数并返回其结果,以便可以发送 POST 响应。我知道这些函数是异步的,我可以用回调或承诺链接它们。
真正的问题是如何让 POST 响应等待 getToken 函数的回调,因为最终目标是将自定义令牌传递回 [=30] 正文中的移动应用程序=] 响应。
任何帮助将不胜感激。
您的 validateCredential
和 getToken
函数已经 async
反过来 returns 承诺,在 POST
函数中等待这些函数发送响应, 你必须创建 POST
函数 async
,然后在调用这两个函数时使用 await
关键字,当你使用 await
函数执行时,等待函数响应 Promise
解析,这里是示例代码。
//Listen for app to POST Credential
app.post('/', async function(request, response) {
console.log('Request Body: ',request.body);
var Credential = request.body;
//Validate Credential
var userId = await validateCredential(Credential) //Waits until userId comes
//Get Authorization Token
var firebaseAuthToken = await getToken(userID) //waits until Token comes
//Return Token for POST Response
response.set('Content-Type','Text');
response.end(firebaseAuthToken);
});
我对 Node.js
比较陌生,我正在创建一个服务器,它将接受来自移动应用程序的 POST
请求,该应用程序的正文包含一个凭据,然后通过 GET
到另一台服务器。如果 GET
响应验证了凭据,则提取 UID 并调用 firebase admin SDK
以创建自定义令牌。下面是一段代码和两个被调用的函数 (a)
验证凭据并 (b)
生成自定义令牌。
//Listen for app to POST Credential
app.post('/', function(request, response) {
console.log('Request Body: ',request.body);
var Credential = request.body;
//Validate Credential
validateCredential(Credential)
//Get Authorization Token
getToken(userID)
//Return Token for POST Response
response.set('Content-Type','Text');
response.end(firebaseAuthToken);
});
//Create listener for POST function
app.listen(port, function() {
console.log('AuthServer is running and listening on port '+port);
});
//Function to Validate Credential
async function validateCredential(crdntl) {
//Call Service to validate Credential received
axios({
method: 'get',
url: 'https://.....',
})
.then(function(response) {
...check credential validation data
})
.catch(function (error) {
console.log('ERROR: Unable to Validate Credential');
//Unable to create Validate Credential so return error message for POST response
return ('ERROR1');
});
}
async function getToken(uid) {
admin.auth().createCustomToken(uid)
.then(function(customToken) {
var AuthToken = customToken;
var decoded = jwt.decode(AuthToken);
console.log('Decoded Token: '+'\n',decoded);
//Return Authorization Token for POST response
return (AuthToken);
})
.catch(function(error) {
console.log('ERROR: Unable to Create Custom Token', error);
//Unable to create Token so return error message for POST response
return ('ERROR2');
});
}
}
我需要返回 validateCredential 函数的结果并将其结果传递给 getToken 函数并返回其结果,以便可以发送 POST 响应。我知道这些函数是异步的,我可以用回调或承诺链接它们。
真正的问题是如何让 POST 响应等待 getToken 函数的回调,因为最终目标是将自定义令牌传递回 [=30] 正文中的移动应用程序=] 响应。 任何帮助将不胜感激。
您的 validateCredential
和 getToken
函数已经 async
反过来 returns 承诺,在 POST
函数中等待这些函数发送响应, 你必须创建 POST
函数 async
,然后在调用这两个函数时使用 await
关键字,当你使用 await
函数执行时,等待函数响应 Promise
解析,这里是示例代码。
//Listen for app to POST Credential
app.post('/', async function(request, response) {
console.log('Request Body: ',request.body);
var Credential = request.body;
//Validate Credential
var userId = await validateCredential(Credential) //Waits until userId comes
//Get Authorization Token
var firebaseAuthToken = await getToken(userID) //waits until Token comes
//Return Token for POST Response
response.set('Content-Type','Text');
response.end(firebaseAuthToken);
});