Search code examples
node.jshosts

How to get local development .test domains working with NodeJS?


With Rails and puma-dev I can easily have multiple projects on my dev machine running on different hosts e.g.

~/development/myapp => http://myapp.test
~/development/myapp2 => http://myapp2.test
~/development/myapp3 => http://myapp3.test

With NodeJS (NextJS etc.) it is

~/development/myapp => http://localhost:3000
~/development/myapp2 => http://localhost:3001
~/development/myapp3 => http://localhost:3003

And I have to manually run npm run dev for each.

How do I get similar functionality to Rails and puma-dev please?

I have tried to edit /etc/hosts but am not clear on how to avoid having a port in the URL e.g. I don't want http://myapp.test:3000 nor do I want to run npm run dev on port 80 or 8080.


Solution

  • Puma-dev is hooking into ports 80/443 by default. This is also where you computer/browser/host happens to look. (i.e. a request to http://localhost is automatically going to look at port 80/http and 443/https).

    If you want this behavior for NextJS, you can put it behind an nginx proxy (http OR https).

    To apply the behavior you're looking for in puma, echo 3000 > ~/.puma-dev/myapp

    From puma-dev docs (https://github.com/puma/puma-dev?tab=readme-ov-file#proxy-support):

    Proxy support Puma-dev can also proxy requests from a nice dev domain to another app. To do so, just write a file (rather than a symlink'd directory) into ~/.puma-dev with the connection information.

    For example, to have port 9292 show up as awesome.test: echo 9292 > ~/.puma-dev/awesome.

    Or to proxy to another host: echo 10.3.1.2:9292 > ~/.puma-dev/awesome-elsewhere.