How to set up local subdomains for Node.js app

There is an awesome website, which someone hosted for all of us.

localtest.me

All requests will be routed to 127.0.0.1, including subdomains. e.g something.localtest.me:3000 will resolve to 127.0.0.1:3000

but, for example, in your Express app, if you do

 app.get('*', (req, res) => {
     console.log(req.subdomains); // [ something ]
 });

you'll get your subdomain


I'm the author of the module :)

For each new subdomain you wish to test locally you must add into your /etc/hosts file. So for example:

localhost is:

127.0.0.1       localhost

a new subdomain would be..

127.0.0.1       sub1.localhost

and another..

127.0.0.1       sub2.localhost

Check out what I have done in the tests.


I had same exact problem and I found a simple solution. Instead of writing sub1.localhost try replacing localhost with lvh.me this is a domain that always resolves to localhost and now whenever you write sub1.lvh.me even though a port like sub1.lvh.me:3000 it will still work.