1
私はexpress.jsを使ってサーバーを開発しています。このサーバーは、github apiにwebhooks内のユーザーのリポジトリを取得するよう要求します。問題は、この要求の戻り値:エラー504を解決するには:express.js(v 4.0.0)のゲートウェイタイムアウト?
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' }
任意のアイデア?どうもありがとう。
[OK]を、コードはかなり長いですが、今私は答えに追加します。 –
2つのサービスはクリアされていますが、Webハンドラの読み込みに問題があります。あなたのWebハンドラでは、処理を完了するたびに(4xxステータスを設定するたびに)応答を終了し、エラーを回避するために関数から戻るように勧めました。 – yeiniel
はい、私はエラーが発生したときにそれを送信します。コントローラーコードを分かりやすくするために切り捨てます。 –