tapio / live-server

A simple development http server with live reload capability.
http://tapiov.net/live-server/
4.44k stars 485 forks source link

localhost instead of 127.0.0.1 #66

Closed jeroencusters closed 8 years ago

jeroencusters commented 9 years ago

Is it possible to open the browser with localhost instead of 127.0.0.1? I tried 'live-server --host=localhost' but this doesn't work.

mattborn commented 8 years ago

:+1:

This affects how I load fonts and whitelisted domains in my font provider. 127.0.0.1 doesn’t load the fonts. localhost does. I could change the whitelists for all of my font kits, but it would be much easier if this just worked out-of-the-box without having to manually change 127.0.0.1 to localhost in the address bar. Otherwise the default open: true feature gets in the way.

In the live-server/index.js file, I see these relevant lines:

101 · LiveServer.start = function(options) {
…
103 · var host = options.host || '0.0.0.0';
…
136 · var address = server.address();
137 · var serveHost = address.address === "0.0.0.0" ? "127.0.0.1" : address.address;
138 · var serveURL = 'http://' + serveHost + ':' + address.port;
…
211 · };

Not sure the best way to fix this without a better understanding of why this is constructed this way to begin with, otherwise I would make a PR.

tapio commented 8 years ago

It was localhost first, but it had problems (see #7). Apparantly there is no one address that works properly everywhere. :(

mattborn commented 8 years ago

@tapio I get that different environments (OS and browsers) and use cases (http server, websockets, livereload, etc.) each have their own concerns and limitations so it’s prudent to provide a sane default, but I think this issue suggests an explicit host value should not be converted back to one of the defaults (either 127.0.0.1 or 0.0.0.0). It should be considered a bug if setting a host value doesn’t work either as a command-line argument or in the ~/.live-server.json.

tapio commented 8 years ago

The code used the resulting IP address of the given host string as the open url. I've now changed it in c94694bd5eb95 to use the user supplied string so that live-server --host=localhost really opens the browser at literally the string "localhost".

mattborn commented 8 years ago

@tapio :heart: