Original Discussion: https://www.pika.dev/npm/snowpack/discuss/412
/cc @deviousdodo thanks for offering to tackle!
I need a host option available in devOptions, so that the browser opens directly at example.local:8080 instead of localhost:8080. Right now I have to manually change the host after starting snowpack. I can create a PR myself if this is something that would be merged.
Sorry for the delay, PR is now created.
@deviousdodo thanks so much for this PR!
I see that in the PR, you added the ability to open a browser with the given hostname. But the actual server is still served at localhost. I wasn鈥檛 able to see a way to easily set that to, say, 0.0.0.0 like you鈥檇 want if you were exposing your dev server to the local network (to preview on another device, etc.).
What are y鈥檃lls thoughts on hostname not changing localhost in the server setup? On the one hand, this would be useful and some have asked about this. But on the other hand, I鈥檓 actually not even sure how you pull this off with Node鈥檚 http.createServer() method. Even servor doesn鈥檛 seem to be capable of this.
It looks like you need to specify the hostname in the listen function and modify your hosts file to point to the loopback (127.0.0.1)
@stramel yes, you'd need to add the hostname to your /etc/hosts file, maybe I should point that out in the README? However, I don't think the listen function needs to change.
@drwpow From what I can tell, dev server doesn't listen specifically on localhost, but it's started without a host parameter: https://github.com/pikapkg/snowpack/blob/87c2fc7c51eb2bca03ce7b85bd1f05cc728e9b4b/src/commands/dev.ts#L792
Which should simply start listening for 0.0.0.0 and everything should be good (including the initial issue you referenced). Am I missing anything?
Yeah, it does listen to ::/0.0.0.0
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
Seems like it's best practice not to bind to 0.0.0.0
Well, it would allow people on the same network to access your dev server. I guess you can see that as a feature or a security issue :) I prefer the flexibility of 0.0.0.0 for a dev server. And another option ip could be added to devOptions with a default to 127.0.0.1 for example - if you think it warrants the extra code.
Either way, it seems like a separate issue from the hostname.
Most helpful comment
@deviousdodo thanks so much for this PR!
I see that in the PR, you added the ability to open a browser with the given hostname. But the actual server is still served at
localhost. I wasn鈥檛 able to see a way to easily set that to, say,0.0.0.0like you鈥檇 want if you were exposing your dev server to the local network (to preview on another device, etc.).What are y鈥檃lls thoughts on
hostnamenot changinglocalhostin the server setup? On the one hand, this would be useful and some have asked about this. But on the other hand, I鈥檓 actually not even sure how you pull this off with Node鈥檚http.createServer()method. Even servor doesn鈥檛 seem to be capable of this.