简体   繁体   中英

Node.js https request work locally but throws timeout when deployed to 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 

You're getting a timeout error Error: connect ETIMEDOUT 200.201.170.21:443 which is suggesting the host is not answering.

Try updating your response.on error to log the error. That should give more insight.

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

You may need the help of whoever maintains the server to open it up to your heroku instance.

To debug you can run heroku run node which will open up a node console on your server. Then you can copy/paste your app code into it. It'll save time rather than deploying each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM