简体   繁体   中英

How to bind a domain name to expressjs application

How to bind a domain name to expressjs application.My application is running on servername:1001 I want to bind it to www.domainname.com. How can I do this in the application.

I'm searching for the same answer.

I have just been using app.use('/', router) with a processor that then checks the domain off the req object.

router.get('*', (req, res, next) => {

  let path = req.path.replace(/^\//g,'').replace(/\/$/g,'');
  let domain = req.headers.host.split(':')[0];
  domain = domain.replace(/^www\./g,'');

});

This is the closest thing I've been able to come with to actually using express routes per domain. On my own, 4 years ago.

Now there seems to be this VHOST module: http://expressjs.com/en/resources/middleware/vhost.html

-Robert

Your DNS record you should use CNAME. As a value you should use the domain your hosting provider gave you ex. myapp.herokuapp.com(which on heroku side point to servername:1001). If you are using hosting like heroku, you should setup to accept traffic for your application for that specific domain. That last configuration is being made on heroku management site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM