我正在使用express.js开发一个服务器,它向github api发出请求以获取webhooks中的用户存储库。问题是这个请求返回:
{'John': {'High <1 day': 2,'Low <1 day': 1,'High >1 day': 1,'Low >1 day': 2,'High >3 days': 3,'Low >3 days': 3},'Mary': {'High <1 day': 5,'Low <1 day': 0,'High >1 day': 5,'Low >1 day': 2,'High >3 days': 1,'Low >3 days': 9}}
我的代码:
服务:
exports.getWebHooks = function getWebHooks(githubtoken,arrRepos){
problem with request: timed out
[error] { [Error: 504: Gateway Timeout]
[error] defaultMessage: 'Gateway Timeout',
[error] message: '504: Gateway Timeout',
[error] code: '504' }
};
由于cmd结果我知道应用程序流程:
var promise = new Hope.Promise();
var arrReposDef = [];
var github = new GitHubApi({
version: "3.0.0",
debug: true,
protocol: "https",
host: "api.github.com", // should be api.github.com for GitHub
timeout: 5000,
headers: {
"user-agent": "Meanstack" // GitHub is happy with a unique user agent
}
});
github.authenticate({
type: "oauth",
token: githubtoken.token
});
async.each(arrRepos,
function(item, callback){
// It runs well
github.repos.getHooks({
user: githubtoken.username,
repo: item.name,
headers: {
"X-GitHub-OTP": "two-factor-code"
}
}, function(err, res) {
if (err) { // it runs wrong 2 times, this the error that I defined up.
console.log(err);
githuberror = true;
}
else{ // it runs well 2 times
if(!res.length){
arrReposDef.push(item);
}
else{
var enc = false;
for(var i = 0; i< res.length; i++){
if(res[i].config !== undefined && res[i].config !== null){
if(res[i].config.url !== undefined && res[i].config.url !== null){
if(res[i].config.url !== config.githubcallback){
enc = true;
}
}
}
}
if(enc) {
arrReposDef.push(item);
}
}
}
callback(); //required
});
},
// 3rd param is the function to call when everything's done
function(err){
if(err){
console.log('Error:' + err);
return promise.done(err,null);
}
// All tasks are done now
else{
return promise.done(null,arrReposDef);
}
}
);
return promise;
有什么想法吗?非常感谢。
答案 0 :(得分:-2)
问题是您的API需要很长时间才能响应。这很可能是由于您的代码中的错误而不是由调用外部API引起的。也许你可以在这里分享一些代码,这样我们就可以尝试找到问题所在。使用超时中间件只是规范应用程序挂起以处理Web请求的情况。
在这里,我更新您的Web请求处理程序,在适当的时候结束响应。
exports.auth = function auth (request, response) {
var userid= request.params.userid;
var username = request.body.username;
var pass = request.body.pass;
var arrRepos = [];
var usertoken = "";
if(userid !== undefined && userid !== null &&
username !== undefined && username !== null ){
Auth(request, response).then(function(error, result) {
if (error) {
response.status(error.code).json({message: error.message});
response.end();
return;
}
if (userid !== result._id){
response.status(401).json({message: 'Unauthorized. You are trying to access with a different userid'});
response.end();
return;
}
githubapiservice.getUserToken(userid, username).then(function (error,result){
if(error){
response.status(error.code).json({message: error.message});
response.end();
return;
}
var newResult = {};
if(result == null){
if(pass == undefined || pass == null || pass == ''){
response.status(400).json({message: 'Bad Request. Missing required parameters'});
response.end();
return;
}
githubapiservice.createToken(userid,username,pass).then(function (error,result){
if(error){
var messageJSON= JSON.parse(error.message);
response.status(error.code).json({message: messageJSON.message});
response.end();
return;
}
if(result == null ){
response.json({message: "No se ha conseguido el token, intentelo de nuevo más tarde"});
response.end();
return;
}
usertoken = result;
githubapiservice.saveUserToken(userid, username, result).then(function (error,result){
if(error){
response.status(error.code).json({message: error.message});
response.end();
return;
}
if(result !== null){
newResult.githubtoken = result;
githubapiservice.getRepositories(result).then(function (error,result){
if(error){
if(error.code == '504'){
response.status(error.code).json({message: "Gateway Timeout"});
response.end();
}
else{
var messageJSON= JSON.parse(error.message);
response.status(error.code).json({message: messageJSON.message});
response.end();
}
return;
}
if (result === undefined || result === null || result.length === 0){
newResult.arrRepos = arrRepos;
response.json(newResult);
response.end();
return;
}
newResult.arrRepos = result;
githubapiservice.getWebHooks(newResult.githubtoken, result).then(function (error,result) {
if (error) {
var messageJSON = JSON.parse(error.message);
response.status(error.code).json({message: messageJSON.message});
}
else {
newResult.arrRepos = result;
response.json(newResult);
}
response.end();
});
});
return;
}
response.status(422).json({message: "Validation Failed"});
response.end();
});
}); /* end create token */
} else {
newResult.githubtoken = result;
githubapiservice.getRepositories(newResult.githubtoken).then(function (error, result) {
if(error){
var messageJSON= JSON.parse(error.message);
response.status(error.code).json({message: messageJSON.message});
response.end();
return;
}
if (result === undefined || result === null || result.length === 0){
newResult.arrRepos = arrRepos;
response.json(newResult);
response.end();
return;
}
githubapiservice.getWebHooks(newResult.githubtoken, result).then(function (error,result) {
if (error) {
var messageJSON = JSON.parse(error.message);
response.status(error.code).json({message: messageJSON.message});
response.end();
return;
}
newResult.arrRepos = result;
response.json(newResult);
response.end();
});
});
}
}); /* end getUserToken */
});
} else {
response.status(400).json({message: 'Bad Request. Missing required parameters'});
response.end();
}
};