简体   繁体   中英

Add domains to NodeJS Express http server on the fly

I'm hosting multiple domains each running the same node app using Express vhost. I start them all using http.createServer

var app = express(); 
require('./app.js')(function(theApp) {

    app.use(express.vhost(domainName1, theApp));
    app.use(express.vhost(domainName2, theApp));
    app.use(express.vhost(domainName3, theApp));
    var d = domain.create();
    d.run(function(){
        http.createServer(app).listen(80);
    }); 
});

Is it possible to do the same thing but be able to add new domains to the server on the fly without having to restart the node application? IE pseudo-code:

  • Do the above
  • Wait for a Domains database record to be added
  • Add a new vhost
  • Restart necessary things

I want to make sure that users of the existing domains get as little down-time as possible (or even none) whilst the new domain is added.

The default Express behavior is to not discriminate based on the Host header. The vhost middleware adds this behavior. You probably could add more middleware whenever you discover a new domain you want to support… or you could just not discriminate on the Host header in the first place :

var app = express(); 
require('./app.js')(function(theApp) {
    app.use(theApp);
    var d = domain.create();
    d.run(function(){
        http.createServer(app).listen(80);
    }); 
});

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