Maxence Pucheu
Maxence Pucheu

Reputation: 1

Nuxt serverInit axios on localhost getting ENOTFOUND

I'm making a website using NuxtJS for the front-end, and Laravel for the backend.

I need to have some data available on every page, so I fetch it using the nuxtServerInit function like so:

async nuxtServerInit({ commit }, { $axios }) {
  const items= await $axios.$get('items')

  commit('saveItems', items)
}

This works fine when pointing to my Laravel app that's already online, but not when I'm running it on my local machine. I get an "get addrinfo ENOTFOUND 3213" error.

My .env file contains the baseURL for axios, so local would be:

API_URL=http://127.0.0.1:8000/v1/

I tried many things but to no avail. It works when using the live backend, and there's no problem with the API_URL as it works on every other function/page apart from the nuxtServerInit one. I tried:

But nothing works. My /etc/host file also correctly contains 127.0.0.1 as I saw it was a solution for a lot of ENOTFOUND errors on local.

I also tried changing the API_URL in my .env file to

API_URL=localhost:8000/v1/

But then I get "ENOTFOUND 8000" instead, or whichever the port I set my Laravel server to.

Please help!

Edit:

After toggling debug on Axios on dev, I found the host name is wrong in the request, which in turns make the URL incorrect:

    reusedSocket: false,
      host: '3213',
      protocol: 'http:',
      _redirectable: [Circular *1],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    _currentUrl: 'http://3213/http://localhost:8000/v1/items',

I don't know where the "3213" comes from. I tried setting default host/hostnames/port values to Axios in nuxt.config.js, but no luck.

Upvotes: 0

Views: 435

Answers (1)

Maxence Pucheu
Maxence Pucheu

Reputation: 1

Solved it randomly. I had a proxy variable setup in my environment variables (Win 10) called "http_proxy". Deleted it, and no more problem.

Upvotes: 0

Related Questions