gpt4 book ai didi

node.js - 如何修复错误 504 : Gateway Timeout in express. js (v 4.0.0)?

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:21 25 4
gpt4 key购买 nike

我正在使用express.js开发一个服务器,它向github api发出请求以在webhooks中获取用户的存储库。问题是这个请求返回:

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' }

有什么想法吗?非常感谢。

最佳答案

问题是您的 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();
}
};

关于node.js - 如何修复错误 504 : Gateway Timeout in express. js (v 4.0.0)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36651701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com