微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node.js – 在Express中指定路由定义中的子域

我是Expressjs和NodeJS的新手,所以我需要有关如何实现这种效果的指示:

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

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

因此,当我请求sub1.domain.com第一个处理程序作出反应时,在sub2.domain.com上我得到第二个处理程序的响应.我已经阅读了有关使用vhost实现此目的的一些问题,但如果我上面描述的内容有效而不是像在vhost中创建多个服务器实例那么我会更高兴.

解决方法

一个快速简单的解决方案是:

app.get('/',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!");

});

参考:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐