Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use localhost:3000 as default in production #274

Open
matthieusieben opened this issue Aug 8, 2019 · 12 comments
Open

Do not use localhost:3000 as default in production #274

matthieusieben opened this issue Aug 8, 2019 · 12 comments

Comments

@matthieusieben
Copy link

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)
@ghost ghost added the cmty:feature-request label Aug 8, 2019
@pi0
Copy link
Member

pi0 commented Aug 8, 2019

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

@matthieusieben
Copy link
Author

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...

@pi0
Copy link
Member

pi0 commented Aug 9, 2019

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

@nzakas
Copy link

nzakas commented Oct 18, 2019

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.

@ricardogobbosouza
Copy link
Member

ricardogobbosouza commented Oct 21, 2019

@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?

@pi0 pi0 removed the hacktoberfest label Oct 21, 2019
@pi0
Copy link
Member

pi0 commented Oct 21, 2019

@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.

@ricardogobbosouza
Copy link
Member

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

@TheAlexLichter
Copy link
Member

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

@pi0
Copy link
Member

pi0 commented Oct 22, 2019

@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.

@TheAlexLichter
Copy link
Member

Ohh, I got it wrong then 🙈

@1isten
Copy link

1isten commented Jan 14, 2020

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)

@greenermoose
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants