How to set up local subdomains for Node.js app

前端 未结 4 403
北海茫月
北海茫月 2021-02-02 15:41

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\'

相关标签:
4条回答
  • 2021-02-02 16:15

    on Ubuntu

    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.

    0 讨论(0)
  • 2021-02-02 16:20

    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.

    0 讨论(0)
  • 2021-02-02 16:22

    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

    0 讨论(0)
  • 2021-02-02 16:31

    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.

    0 讨论(0)
提交回复
热议问题