我试图将我的node.js应用程序部署到Heroku,但是当我试图使后端请求时,我得到一个Error: connect ECONNREFUSED 127.0.0.1:80
我相信我已经正确设置了我的端口:
const port = process.env.PORT || 8000;
const server = app.listen(port, () => {
console.log(`App running on port ${port}`);
});
这是我的axios方法:
exports.home = catchAsync(async (req, res, next) => {
const upcoming = await axios({
method: 'GET',
url: '/api/v1/shifts/upcoming',
params: {
limit: 4,
},
});
这是我的日志:
2021-02-10T16:53:56.341894+00:00 app[web.1]: ERROR � Error: connect ECONNREFUSED 127.0.0.1:80
2021-02-10T16:53:56.341906+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
2021-02-10T16:53:56.341906+00:00 app[web.1]: errno: 'ECONNREFUSED',
2021-02-10T16:53:56.341907+00:00 app[web.1]: code: 'ECONNREFUSED',
2021-02-10T16:53:56.341907+00:00 app[web.1]: syscall: 'connect',
2021-02-10T16:53:56.341907+00:00 app[web.1]: address: '127.0.0.1',
2021-02-10T16:53:56.341908+00:00 app[web.1]: port: 80,
2021-02-10T16:53:56.341909+00:00 app[web.1]: config: {
2021-02-10T16:53:56.341909+00:00 app[web.1]: url: '/api/v1/shifts/upcoming',
2021-02-10T16:53:56.341910+00:00 app[web.1]: method: 'get',
2021-02-10T16:53:56.341910+00:00 app[web.1]: headers: {
2021-02-10T16:53:56.341911+00:00 app[web.1]: Accept: 'application/json, text/plain, */*',
2021-02-10T16:53:56.341911+00:00 app[web.1]: 'User-Agent': 'axios/0.21.1'
2021-02-10T16:53:56.341911+00:00 app[web.1]: },
2021-02-10T16:53:56.341912+00:00 app[web.1]: params: { limit: 4 },
2021-02-10T16:53:56.341912+00:00 app[web.1]: transformRequest: [ [Function: transformRequest] ],
2021-02-10T16:53:56.341912+00:00 app[web.1]: transformResponse: [ [Function: transformResponse] ],
2021-02-10T16:53:56.341913+00:00 app[web.1]: timeout: 0,
2021-02-10T16:53:56.341913+00:00 app[web.1]: adapter: [Function: httpAdapter],
2021-02-10T16:53:56.341914+00:00 app[web.1]: xsrfCookieName: 'XSRF-TOKEN',
2021-02-10T16:53:56.341914+00:00 app[web.1]: xsrfHeaderName: 'X-XSRF-TOKEN',
2021-02-10T16:53:56.341914+00:00 app[web.1]: maxContentLength: -1,
2021-02-10T16:53:56.341915+00:00 app[web.1]: maxBodyLength: -1,
2021-02-10T16:53:56.341915+00:00 app[web.1]: validateStatus: [Function: validateStatus],
2021-02-10T16:53:56.341915+00:00 app[web.1]: data: undefined
2021-02-10T16:53:56.341916+00:00 app[web.1]: },
2021-02-10T16:53:56.341916+00:00 app[web.1]: request: Writable {
2021-02-10T16:53:56.341916+00:00 app[web.1]: _writableState: WritableState {
2021-02-10T16:53:56.341917+00:00 app[web.1]: objectMode: false,
2021-02-10T16:53:56.341917+00:00 app[web.1]: highWaterMark: 16384,
2021-02-10T16:53:56.341917+00:00 app[web.1]: finalCalled: false,
2021-02-10T16:53:56.341918+00:00 app[web.1]: needDrain: false,
2021-02-10T16:53:56.341918+00:00 app[web.1]: ending: false,
2021-02-10T16:53:56.341918+00:00 app[web.1]: ended: false,
2021-02-10T16:53:56.341919+00:00 app[web.1]: finished: false,
2021-02-10T16:53:56.341919+00:00 app[web.1]: destroyed: false,
2021-02-10T16:53:56.341919+00:00 app[web.1]: decodeStrings: true,
2021-02-10T16:53:56.341919+00:00 app[web.1]: defaultEncoding: 'utf8',
2021-02-10T16:53:56.341920+00:00 app[web.1]: length: 0,
2021-02-10T16:53:56.341920+00:00 app[web.1]: writing: false,
2021-02-10T16:53:56.341920+00:00 app[web.1]: corked: 0,
2021-02-10T16:53:56.341921+00:00 app[web.1]: sync: true,
2021-02-10T16:53:56.341921+00:00 app[web.1]: bufferProcessing: false,
2021-02-10T16:53:56.341921+00:00 app[web.1]: onwrite: [Function: bound onwrite],
2021-02-10T16:53:56.341922+00:00 app[web.1]: writecb: null,
2021-02-10T16:53:56.341922+00:00 app[web.1]: writelen: 0,
2021-02-10T16:53:56.341922+00:00 app[web.1]: afterWriteTickInfo: null,
2021-02-10T16:53:56.341923+00:00 app[web.1]: bufferedRequest: null,
2021-02-10T16:53:56.341923+00:00 app[web.1]: lastBufferedRequest: null,
2021-02-10T16:53:56.341923+00:00 app[web.1]: pendingcb: 0,
2021-02-10T16:53:56.341924+00:00 app[web.1]: prefinished: false,
2021-02-10T16:53:56.341924+00:00 app[web.1]: errorEmitted: false,
2021-02-10T16:53:56.341924+00:00 app[web.1]: emitClose: true,
2021-02-10T16:53:56.341925+00:00 app[web.1]: autoDestroy: false,
2021-02-10T16:53:56.341925+00:00 app[web.1]: bufferedRequestCount: 0,
2021-02-10T16:53:56.341925+00:00 app[web.1]: corkedRequestsFree: [Object]
2021-02-10T16:53:56.341925+00:00 app[web.1]: },
2021-02-10T16:53:56.341926+00:00 app[web.1]: writable: true,
2021-02-10T16:53:56.341926+00:00 app[web.1]: _events: [Object: null prototype] {
2021-02-10T16:53:56.341926+00:00 app[web.1]: response: [Function: handleResponse],
2021-02-10T16:53:56.341927+00:00 app[web.1]: error: [Function: handleRequestError]
2021-02-10T16:53:56.341927+00:00 app[web.1]: },
2021-02-10T16:53:56.341927+00:00 app[web.1]: _eventsCount: 2,
2021-02-10T16:53:56.341928+00:00 app[web.1]: _maxListeners: undefined,
2021-02-10T16:53:56.341928+00:00 app[web.1]: _options: {
2021-02-10T16:53:56.341928+00:00 app[web.1]: maxRedirects: 21,
2021-02-10T16:53:56.341929+00:00 app[web.1]: maxBodyLength: 10485760,
2021-02-10T16:53:56.341929+00:00 app[web.1]: protocol: 'http:',
2021-02-10T16:53:56.341929+00:00 app[web.1]: path: '/api/v1/shifts/upcoming?limit=4',
2021-02-10T16:53:56.341930+00:00 app[web.1]: method: 'GET',
2021-02-10T16:53:56.341930+00:00 app[web.1]: headers: [Object],
2021-02-10T16:53:56.341930+00:00 app[web.1]: agent: undefined,
2021-02-10T16:53:56.341930+00:00 app[web.1]: agents: [Object],
2021-02-10T16:53:56.341931+00:00 app[web.1]: auth: undefined,
2021-02-10T16:53:56.341931+00:00 app[web.1]: hostname: null,
2021-02-10T16:53:56.341931+00:00 app[web.1]: port: null,
2021-02-10T16:53:56.341932+00:00 app[web.1]: nativeProtocols: [Object],
2021-02-10T16:53:56.341932+00:00 app[web.1]: pathname: '/api/v1/shifts/upcoming',
2021-02-10T16:53:56.341932+00:00 app[web.1]: search: '?limit=4'
2021-02-10T16:53:56.341933+00:00 app[web.1]: },
2021-02-10T16:53:56.341933+00:00 app[web.1]: _ended: true,
2021-02-10T16:53:56.341933+00:00 app[web.1]: _ending: true,
2021-02-10T16:53:56.341933+00:00 app[web.1]: _redirectCount: 0,
2021-02-10T16:53:56.341934+00:00 app[web.1]: _redirects: [],
2021-02-10T16:53:56.341934+00:00 app[web.1]: _requestBodyLength: 0,
2021-02-10T16:53:56.341934+00:00 app[web.1]: _requestBodyBuffers: [],
2021-02-10T16:53:56.341935+00:00 app[web.1]: _onNativeResponse: [Function],
2021-02-10T16:53:56.341935+00:00 app[web.1]: _currentRequest: ClientRequest {
2021-02-10T16:53:56.341935+00:00 app[web.1]: _events: [Object: null prototype],
2021-02-10T16:53:56.341936+00:00 app[web.1]: _eventsCount: 7,
2021-02-10T16:53:56.341936+00:00 app[web.1]: _maxListeners: undefined,
2021-02-10T16:53:56.341936+00:00 app[web.1]: outputData: [],
2021-02-10T16:53:56.341937+00:00 app[web.1]: outputSize: 0,
2021-02-10T16:53:56.341938+00:00 app[web.1]: writable: true,
2021-02-10T16:53:56.341938+00:00 app[web.1]: _last: true,
2021-02-10T16:53:56.341939+00:00 app[web.1]: chunkedEncoding: false,
2021-02-10T16:53:56.341939+00:00 app[web.1]: shouldKeepAlive: false,
2021-02-10T16:53:56.341939+00:00 app[web.1]: _defaultKeepAlive: true,
2021-02-10T16:53:56.341940+00:00 app[web.1]: useChunkedEncodingByDefault: false,
2021-02-10T16:53:56.341940+00:00 app[web.1]: sendDate: false,
2021-02-10T16:53:56.341940+00:00 app[web.1]: _removedConnection: false,
2021-02-10T16:53:56.341941+00:00 app[web.1]: _removedContLen: false,
2021-02-10T16:53:56.341941+00:00 app[web.1]: _removedTE: false,
2021-02-10T16:53:56.341942+00:00 app[web.1]: _contentLength: 0,
2021-02-10T16:53:56.341942+00:00 app[web.1]: _hasBody: true,
2021-02-10T16:53:56.341942+00:00 app[web.1]: _trailer: '',
2021-02-10T16:53:56.341943+00:00 app[web.1]: finished: true,
2021-02-10T16:53:56.341943+00:00 app[web.1]: _headerSent: true,
2021-02-10T16:53:56.341943+00:00 app[web.1]: socket: [Socket],
2021-02-10T16:53:56.341944+00:00 app[web.1]: connection: [Socket],
2021-02-10T16:53:56.341944+00:00 app[web.1]: _header: 'GET /api/v1/shifts/upcoming?limit=4 HTTP/1.1rn' +
2021-02-10T16:53:56.341944+00:00 app[web.1]: 'Accept: application/json, text/plain, */*rn' +
2021-02-10T16:53:56.341945+00:00 app[web.1]: 'User-Agent: axios/0.21.1rn' +
2021-02-10T16:53:56.341945+00:00 app[web.1]: 'Host: localhostrn' +
2021-02-10T16:53:56.341945+00:00 app[web.1]: 'Connection: closern' +
2021-02-10T16:53:56.341946+00:00 app[web.1]: 'rn',
2021-02-10T16:53:56.341946+00:00 app[web.1]: _keepAliveTimeout: 0,
2021-02-10T16:53:56.341946+00:00 app[web.1]: _onPendingData: [Function: noopPendingOutput],
2021-02-10T16:53:56.341946+00:00 app[web.1]: agent: [Agent],
2021-02-10T16:53:56.341947+00:00 app[web.1]: socketPath: undefined,
2021-02-10T16:53:56.341947+00:00 app[web.1]: method: 'GET',
2021-02-10T16:53:56.341947+00:00 app[web.1]: insecureHTTPParser: undefined,
2021-02-10T16:53:56.341948+00:00 app[web.1]: path: '/api/v1/shifts/upcoming?limit=4',
2021-02-10T16:53:56.341948+00:00 app[web.1]: _ended: false,
2021-02-10T16:53:56.341948+00:00 app[web.1]: res: null,
2021-02-10T16:53:56.341948+00:00 app[web.1]: aborted: false,
2021-02-10T16:53:56.341949+00:00 app[web.1]: timeoutCb: null,
2021-02-10T16:53:56.341949+00:00 app[web.1]: upgradeOrConnect: false,
2021-02-10T16:53:56.341949+00:00 app[web.1]: parser: null,
2021-02-10T16:53:56.341950+00:00 app[web.1]: maxHeadersCount: null,
2021-02-10T16:53:56.341950+00:00 app[web.1]: reusedSocket: false,
2021-02-10T16:53:56.341950+00:00 app[web.1]: host: 'localhost',
2021-02-10T16:53:56.341951+00:00 app[web.1]: protocol: 'http:',
2021-02-10T16:53:56.341951+00:00 app[web.1]: _redirectable: [Circular],
2021-02-10T16:53:56.341951+00:00 app[web.1]: [Symbol(kCapture)]: false,
2021-02-10T16:53:56.341952+00:00 app[web.1]: [Symbol(kNeedDrain)]: false,
2021-02-10T16:53:56.341952+00:00 app[web.1]: [Symbol(corked)]: 0,
2021-02-10T16:53:56.341952+00:00 app[web.1]: [Symbol(kOutHeaders)]: [Object: null prototype]
2021-02-10T16:53:56.341953+00:00 app[web.1]: },
2021-02-10T16:53:56.341953+00:00 app[web.1]: _currentUrl: 'http:/api/v1/shifts/upcoming?limit=4',
2021-02-10T16:53:56.341954+00:00 app[web.1]: [Symbol(kCapture)]: false
2021-02-10T16:53:56.341954+00:00 app[web.1]: },
2021-02-10T16:53:56.341954+00:00 app[web.1]: response: undefined,
2021-02-10T16:53:56.341955+00:00 app[web.1]: isAxiosError: true,
2021-02-10T16:53:56.341955+00:00 app[web.1]: toJSON: [Function: toJSON],
2021-02-10T16:53:56.341955+00:00 app[web.1]: statusCode: 500,
2021-02-10T16:53:56.341964+00:00 app[web.1]: status: 'error'
2021-02-10T16:53:56.341965+00:00 app[web.1]: }
我注意到当前的URL是完全错误的:
_currentUrl: 'http:/api/v1/shifts/upcoming?limit=4',
由于某些原因没有使用我的heroku url。我被困住了,有人能帮帮我吗?
如果你没有在URL参数中传递绝对URL,如-
{
...
url: 'http://example.org/api/v1/shifts/upcoming',
...
}
必须传递一个baseUrl参数,如-
{
...
baseUrl: 'http://example.org/api/',
url: 'v1/shifts/upcoming',
...
}
您可以参考axios文档- https://www.npmjs.com/package/axios