Node.js https request work locally but throws timeout when deployed to heroku

时间:2018-03-25 21:18:22

标签: javascript node.js heroku

I have an node.js app that access a third party api. When I run the app locally it works normally, but when I deploy it to heroku it throws a timeout error when trying to access the api.

Here's the code of my app:

var express = require("express");
var bodyParser = require("body-parser");
var https = require("https");
var querystring = require('querystring');
var cookieParser = require('cookie-parser')

var app = express();
app.use(bodyParser.json());
app.use(cookieParser());

var options = {
    host: "200.201.170.21",
    port: 443,
    path: '/appFgtsTrabalhador/invoke',
    method: 'POST',
    rejectUnauthorized:false,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
};

var req = https.request(options, function(response) {
    response.setEncoding('utf8');
    response.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });

    response.on('error', function(err) {
        console.log('ERROR');
    });

});

req.end();

And here's the heroku log:

2018-03-25T21:37:56.821829+00:00 app[web.1]: events.js:183
2018-03-25T21:37:56.821851+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2018-03-25T21:37:56.821853+00:00 app[web.1]:       ^
2018-03-25T21:37:56.821854+00:00 app[web.1]: 
2018-03-25T21:37:56.821856+00:00 app[web.1]: Error: connect ETIMEDOUT 200.201.170.21:443
2018-03-25T21:37:56.821857+00:00 app[web.1]:     at Object._errnoException (util.js:1022:11)
2018-03-25T21:37:56.821859+00:00 app[web.1]:     at _exceptionWithHostPort (util.js:1044:20)
2018-03-25T21:37:56.821862+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
2018-03-25T21:37:56.830314+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-03-25T21:37:56.830785+00:00 app[web.1]: npm ERR! errno 1
2018-03-25T21:37:56.836295+00:00 app[web.1]: npm ERR! xxxx@0.0.0 start: `node server.js`
2018-03-25T21:37:56.836500+00:00 app[web.1]: npm ERR! Exit status 1
2018-03-25T21:37:56.836785+00:00 app[web.1]: npm ERR! 
2018-03-25T21:37:56.837005+00:00 app[web.1]: npm ERR! Failed at the xxxx0.0.0 start script.
2018-03-25T21:37:56.837209+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-03-25T21:37:56.869686+00:00 app[web.1]: 
2018-03-25T21:37:56.869958+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-03-25T21:37:56.870133+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-03-25T21_37_56_839Z-debug.log

1 个答案:

答案 0 :(得分:0)

您收到超时错误Error: connect ETIMEDOUT 200.201.170.21:443,表示主持人没有回复。

尝试更新response.on error以记录错误。这应该提供更多的见解。

response.on('error', function(err) {
    console.log('ERROR', err);
});

您可能需要维护服务器的人的帮助才能将其打开到您的heroku实例。

要进行调试,您可以运行heroku run node,这将打开服务器上的节点控制台。然后,您可以将应用程序代码复制/粘贴到其中。它可以节省时间,而不是每次都进行部署。