Axios-module: Do not use localhost:3000 as default in production

Created on 8 Aug 2019  路  12Comments  路  Source: nuxt-community/axios-module

What problem does this feature solve?

If eveything else fails, baseURL will default to http://localhost:3000.

While this is nice in dev environement, it might cause production environement to use http://localhost:3000 as default, if not configured properly.

This lead to error in my deployment because I didn't configure the module correctly but din't realise my mistake until it was too late.

I believe that a better behaviour would be to throw ASAP in that case, preventing the deployment of faulty code.

What does the proposed changes look like?

let defaultPort =
  // ...
  (process.env.NODE_ENV === 'development' && 3000)

let defaultHost =
  // ...
  (process.env.NODE_ENV === 'development' && 'localhost')

if (!defaultPort || !defaultHost) throw new Error('Configuration error')

This feature request is available on Nuxt community (#c267)
feature-request

All 12 comments

Hi. I see the problem but the fact is that we cannot predict deployment type during build to find out correct URL. One thing i would suggest is using proxy module which serves API on same domain/port for client-side

One could argue that not using any default (in dev and prod) would be better as showing an error in dev will ensure that the developer is aware of the config he will need to setup in prod.

But that would be a breaking change...

It depends on use case. Not always defaults are required. When using baseURL or absolute URLs for making requests is possible.

This is a really confusing default behavior, it took me a while to figure out why my API calls were failing. I agree with @matthieusieben -- when I say /api/foo, it is logical to expect that this request will go to the same host/port as the page that is calling it.

@pi0 Do you think we should throw a warning in production mode if the host is localhost and baseURL is not provided or proxy is not configured?

@ricardogobbosouza I'm not sure. Because one may just register a serverMiddleware to serve API requests without any proxy and relative URL. For production (server-side) http://localhost:3000 makes sense as we have to make a request to the same server.

Well remembered, we can't do everything for the user :grimacing:

@pi0 Couldn't we align the baseURL with what the port + host is set to in dev mode? 馃

@manniL Indeed baseURL is auto-generated with port/host of the listener server if not provided:

https://github.com/nuxt-community/axios-module/blob/dev/lib/module.js#L17

This issue is requesting to make setting baseURL manually mandatory for non-dev and not using server host/port to generate it.

Ohh, I got it wrong then 馃檲

proxy module

Hi @pi0 may I know if the proxy module only works with nuxt default server? I tried it with a nuxt app created via create-nuxt-app with Choose custom server framework set to Express and the proxy seems not working (#c33)

I may not be understanding the code, but I think this issue bit me. These two configurations in nuxt.config.js work fine:

// Server Configuration
server: {
// use defaults (localhost:3000)
},

or

// Server Configuration
server: {
host: '192.168.1.6'
},

This one breaks:

// Server Configuration
server: {
host: '0.0.0.0'
},

I think because of
https://github.com/nuxt-community/axios-module/blob/dev/lib/module.js#L35

/* istanbul ignore if */
if (defaultHost === '0.0.0.0') {
defaultHost = 'localhost'
}

I'm getting errors when my dev machine gets an IP address but axios defaults baseURL to localhost not the IP address that is assigned when you use 0.0.0.0 in the configuration.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanwash picture seanwash  路  6Comments

altafsayani picture altafsayani  路  3Comments

kaboume picture kaboume  路  4Comments

ealves-pt picture ealves-pt  路  3Comments

ruudboon picture ruudboon  路  5Comments