Strapi - Axios request on SSR fails with "connect ECONNREFUSED", but CSR works well

System Information
  • Strapi Version: 4.3.0
  • Operating System: Win 64
  • Database: Postgres
  • Node Version: 17
  • NPM Version: 8.1.0
  • Yarn Version: 1.22.19

Request to Strapi during SSR fails for an unknown reason only during SSR. The SSR request hits the Strapi endpoint, I can see the data about to be returned from the controller. I can even see the data if I open the request URL in the browser. But something happens and the request fails.

err :>>  AxiosError: connect ECONNREFUSED ::1:1337
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
  port: 1337,
  address: '::1',
  syscall: 'connect',
  code: 'ECONNREFUSED',
  errno: -4078,
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function: httpAdapter],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function] },
    validateStatus: [Function: validateStatus],
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.27.2'
    },
    baseURL: 'http://localhost:1337',
    url: '/api/reviews',
    params: { locale: undefined, populate: '*', sort: 'createdAt:desc' },     
    method: 'get',
    data: undefined
  },
  request: <ref *1> Writable {
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: null,
      buffered: [],
      bufferedIndex: 0,
      allBuffers: true,
      allNoop: true,
      pendingcb: 0,
      constructed: true,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      errored: null,
      closed: false,
      closeEmitted: false,
      [Symbol(kOnFinished)]: []
    },
    _events: [Object: null prototype] {
      response: [Function: handleResponse],
      error: [Function: handleRequestError],
      socket: [Function: handleRequestSocket]
    },
    _eventsCount: 3,
    _maxListeners: undefined,
    _options: {
      maxRedirects: 21,
      maxBodyLength: 10485760,
      protocol: 'http:',
      path: '/api/reviews?populate=*&sort=createdAt:desc',
      method: 'GET',
      headers: [Object],
      agent: undefined,
      agents: [Object],
      auth: undefined,
      hostname: 'localhost',
      port: '1337',
      nativeProtocols: [Object],
      pathname: '/api/reviews',
      search: '?populate=*&sort=createdAt:desc'
    },
    _ended: true,
    _ending: true,
    _redirectCount: 0,
    _redirects: [],
    _requestBodyLength: 0,
    _requestBodyBuffers: [],
    _onNativeResponse: [Function (anonymous)],
    _currentRequest: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      socket: [Socket],
      _header: 'GET /api/reviews?populate=*&sort=createdAt:desc HTTP/1.1\r\n' 
+
        'Accept: application/json, text/plain, */*\r\n' +
        'User-Agent: axios/0.27.2\r\n' +
        'Host: localhost:1337\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/reviews?populate=*&sort=createdAt:desc',
      _ended: false,
      res: null,
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'localhost',
      protocol: 'http:',
      _redirectable: [Circular *1],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    _currentUrl: 'http://localhost:1337/api/reviews?populate=*&sort=createdAt:desc',
    [Symbol(kCapture)]: false
  }
}

The request is performed like this:

import { API_URL } from '../config';
import { isJsonString } from '../utils/cookie';
import axios from "axios"

export async function GET(ctx) {
  const { params } = ctx
  const { lang } = params

  try {
    const result = await axios({
      baseURL: API_URL,
      url: '/api/reviews',
      params: {
        locale: lang,
        populate: '*',
        sort: 'createdAt:desc'
      }
    })
    console.log('result :>> ', result);

    return {
      status: 200,
      body: result.data,
    }
  } catch (err: any) {
    console.log('err :>> ', err);
    return {
      status: 500,
      body: {
        error:
          isJsonString(err.response?.data) || typeof err.response?.data === "object"
            ? err.response?.data
            : "DB Error",
      },
    }
  }
}

What could possibly be a reason for this? I even tried to set header same as in the browser call - same result

Solved, the issue related to Node V17, which has a breaking change related to DNS lookup.

Specifying 127.0.0.1:1337 instead of localhost has solved the issue

1 Like

Thanks @thousandsofraccoons - this helped me out!

This work for me
Thank you