node.js - Specifying a subdomain in the route definition in Express -


i'm new expressjs , nodejs in general, need directions on how achieve effect:

app.get('/', 'sub1.domain.com', function(req, res) {      res.send("this sub1 response!");  });  app.get('/', 'sub2.domain.com', function(req, res) {     res.send("this sub2 response!"); } 

so when request sub1.domain.com first handler reacts , on sub2.domain.com response second handler. i've read questions on using vhost purpose, i'd more happy if described above worked rather creating multiple server instances in vhost.

a quick , simple solution is:

app.get('/', function(req, res) {    var hostname = req.headers.host.split(":")[0];    if(hostname == "sub1.domain.com")     res.send("this sub1 response!");   else if(hostname == "sub2.domain.com")     res.send("this sub2 response!");  }); 

reference:

http://code4node.com/snippet/http-proxy-with-custom-routing


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -