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

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...