1
我正在开发一个express.js服务器,它向github api发出请求以获取webhooks中的用户存储库。的问题是,这种请求返回:如何修复express.js(v 4.0.0)中的错误504:网关超时?
problem with request: timed out
[error] { [Error: 504: Gateway Timeout]
[error] defaultMessage: 'Gateway Timeout',
[error] message: '504: Gateway Timeout',
[error] code: '504' }
我的代码:
服务:
exports.getWebHooks =函数getWebHooks(githubtoken,arrRepos){
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;
};
我知道应用程序流感谢CMD结果:
REQUEST: { host: 'api.github.com',
port: 443,
path: '/repos/1izpena/angularProject/hooks?access_token=secret',
method: 'get',
headers:
{ host: 'api.github.com',
'content-length': '0',
'x-github-otp': 'two-factor-code',
'user-agent': 'Meanstack',
accept: 'application/vnd.github.v3+json' } }
entro en webhooks, dentro del callback
REQUEST: { host: 'api.github.com',
port: 443,
path: '/repos/1izpena/ionicProject/hooks?access_token=secret',
method: 'get',
headers:
{ host: 'api.github.com',
'content-length': '0',
'x-github-otp': 'two-factor-code',
'user-agent': 'Meanstack',
accept: 'application/vnd.github.v3+json' } }
entro en webhooks, dentro del callback
REQUEST: { host: 'api.github.com',
port: 443,
path: '/repos/1izpena/ionicProjectDef/hooks?access_token=secret',
method: 'get',
headers:
{ host: 'api.github.com',
'content-length': '0',
'x-github-otp': 'two-factor-code',
'user-agent': 'Meanstack',
accept: 'application/vnd.github.v3+json' } }
entro en webhooks, dentro del callback
REQUEST: { host: 'api.github.com',
port: 443,
path: '/repos/1izpena/RestAPI/hooks?access_token=secret',
method: 'get',
headers:
{ host: 'api.github.com',
'content-length': '0',
'x-github-otp': 'two-factor-code',
'user-agent': 'Meanstack',
accept: 'application/vnd.github.v3+json' } }
STATUS: 200
HEADERS: {"server":"GitHub.com","date":"Fri, 15 Apr 2016 18:56:08 GMT","content-type":"application/json; charset=utf-8","content-length":"2","connection":"close","status":"200 OK"..."x-github-request-id":""}
entro en webhooks, no hay errores
entro en webhooks, en if
STATUS: 200
HEADERS: {"server":"GitHub.com","date":"Fri, 15 Apr 2016 18:56:08 GMT","content-type":"application/json; charset=utf-8","content-length":"2","connection":"close","status":"200 OK"...,"x-github-request-id":""}
entro en webhooks, no hay errores
entro en webhooks, en if
problem with request: timed out
[error] { [Error: 504: Gateway Timeout]
[error] defaultMessage: 'Gateway Timeout',
[error] message: '504: Gateway Timeout',
[error] code: '504' } null 1izpena
{ [Error: 504: Gateway Timeout]
defaultMessage: 'Gateway Timeout',
message: '504: Gateway Timeout',
code: '504' }
problem with request: timed out
[error] { [Error: 504: Gateway Timeout]
[error] defaultMessage: 'Gateway Timeout',
[error] message: '504: Gateway Timeout',
[error] code: '504' } null 1izpena
{ [Error: 504: Gateway Timeout]
defaultMessage: 'Gateway Timeout',
message: '504: Gateway Timeout',
code: '504' }
任何想法?非常感谢。
好吧,代码很长,但现在我将它添加到答案中。 –
好吧,这两项服务是明确的,但我无法阅读您的Web处理程序。在您的Web处理程序中,我建议您在每次完成处理时(每次设置4xx状态)都结束响应并从函数返回以避免出现任何错误。 – yeiniel
是的,当它发生时,我会发送错误回复。我将削减控制器代码,使其更容易理解。 –