我有一个访问第三方 API 的 Node.js 应用程序。当我在本地运行该应用程序时,它可以正常工作,但是当我将其部署到 heroku 时,它在尝试访问 api 时会抛出超时错误。
这是我的应用程序的代码:
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();
这是 Heroku 日志:
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
最佳答案
您收到超时错误Error: connect ETIMEDOUT 200.201.170.21:443
,这表明主机没有应答。
尝试更新您的 response.on error
以记录错误。这应该会提供更多见解。
response.on('error', function(err) {
console.log('ERROR', err);
});
您可能需要维护服务器的人的帮助才能将其开放给您的heroku实例。
要进行调试,您可以运行 heroku run node
,这将在您的服务器上打开一个 Node 控制台。然后您可以将应用程序代码复制/粘贴到其中。与每次都进行部署相比,这会节省时间。
关于javascript - Node.js https 请求在本地工作,但部署到 heroku 时抛出超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49481033/