axios 請求錯誤:使用 Heroku 的 ECONNREFUSED (axios request Error: ECONNREFUSED with Heroku)


問題描述

axios 請求錯誤:使用 Heroku 的 ECONNREFUSED (axios request Error: ECONNREFUSED with Heroku)

我嘗試將我的 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.1\r\n' +
2021‑02‑10T16:53:56.341944+00:00 app[web.1]: 'Accept: application/json, text/plain, */*\r\n' +
2021‑02‑10T16:53:56.341945+00:00 app[web.1]: 'User‑Agent: axios/0.21.1\r\n' +
2021‑02‑10T16:53:56.341945+00:00 app[web.1]: 'Host: localhost\r\n' +
2021‑02‑10T16:53:56.341945+00:00 app[web.1]: 'Connection: close\r\n' +
2021‑02‑10T16:53:56.341946+00:00 app[web.1]: '\r\n',
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 網址。我被困在這個問題上,有人可以幫助我嗎?


參考解法

方法 1:

If you aren't passing an absolute URL in url parameter, like ‑

{
...
url: 'http://example.org/api/v1/shifts/upcoming', 
...
}

you have to pass a baseUrl parameter like ‑

{
...
baseUrl: 'http://example.org/api/',
url: 'v1/shifts/upcoming',
...
}

You can refer to axios documentation ‑ https://www.npmjs.com/package/axios

(by Kris GergovMadhav Appaneni)

參考文件

  1. axios request Error: ECONNREFUSED with Heroku (CC BY‑SA 2.5/3.0/4.0)

#Axios #mongoDB #node.js #deployment #Heroku






相關問題

如何從 Contentful 中獲取單個條目並按字段值查詢? (How to fetch a single entry from Contentful and query by field value?)

VUE 中的 Axios 承諾失敗,無法讀取未定義的屬性“toUpperCase” (Axios promise in VUE failed with Cannot read property 'toUpperCase' of undefined)

Symfony 4 - 使用 Axios 提交表單? (Symfony 4 - Submit form with Axios?)

Laravel:帶有axios的表單不保存 (Laravel: form with axios not saving)

使用 axios 將用戶圖像上傳到strapi (uploading user image to strapi using axios)

我的 module.exports 返回為 [object Object] (My module.exports return as [object Object])

即使驗證要求無效,數據仍在發送,解決此問題的最佳方法是什麼? (Data is sending even if validation requirements are not valid, whats the best way to approach this?)

如何等到使用 Axios 上傳文件 (How to wait until file is uploaded using Axios)

axios 請求錯誤:使用 Heroku 的 ECONNREFUSED (axios request Error: ECONNREFUSED with Heroku)

為什麼我的 axios 調用即使在解決之後仍返回 undefined? (Why does my axios call return undefined even after then is resolved?)

為什麼在使用 axios.post 時我的頁面上有一個 OBJECT PROMISE (Why i have a OBJECT PROMISE on my page when using axios.post)

如何在使用反應測試庫單擊父組件上的按鈕後測試子組件呈現文本? (How to test the child component render text after the button on the parent component click with react testing library?)







留言討論