I am running an express app on node.js. The app uses the express-subdomain module to help handle routes for two different subdomains (sub1.example.com and sub2.example.com). I\'
For creating subdomains for localhost you just need to follow 2 simple steps.
Open your terminal by pressing CTRL + ALT + T
then run the following commands:
sudo vi hosts
sudo -i gedit /etc/hosts # to edit /etc/hosts file
Once you run 2nd command /etc/hosts
file will open and now this is the place where you need to define subdomains.
Example: localhost
is:
127.0.0.1 //our localhost
define new subdomain:
127.0.0.1 example.localhost # first
and another..
127.0.0.1 demo.localhost #second
that's it. Hope this was helpful.
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.
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.