Specifying a subdomain in the route definition in Express

旧城冷巷雨未停 提交于 2019-12-03 16:03:54

A quick and simple solution is:

app.get('/', function(req, res) {

  var hostname = req.headers.host.split(":")[0];

  if(hostname == "sub1.domain.com")
    res.send("this is sub1 response!");
  else if(hostname == "sub2.domain.com")
    res.send("this is sub2 response!");

});

Reference:

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

Or you can simply use npm package subdomain, it take cares of your subdomain routes. Also similar to that you can check out Wilson's project on subdomain-handler.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!