在Express中的路由定义中指定子域 [英] Specifying a subdomain in the route definition in Express
本文介绍了在Express中的路由定义中指定子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是ExpressJS和NodeJS的新手,所以我需要指导如何达到这个效果:
I'm new to ExpressJS and NodeJS in general, so I need directions on how to achieve this effect:
app.get('/', 'sub1.domain.com', function(req, res) {
res.send("this is sub1 response!");
});
app.get('/', 'sub2.domain.com', function(req, res) {
res.send("this is sub2 response!");
}
所以当我要求 sub1.domain.com
第一个处理程序反应,在 sub2.domain.com
我得到第二个处理程序的响应我已经阅读了关于使用vhost为此目的的一些问题但是如果我上面描述的那样工作,而不是创建多个服务器实例(如vhost),我会更加高兴。
So that when I request sub1.domain.com
the first handler reacts and on sub2.domain.com
I get response from second handler. I've read some questions on SO about using vhost for this purpose, but I'd be more happy if what I described above worked rather than creating multiple server instances like in vhost.
推荐答案
快速简单的解决方案是:
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!");
});
参考:
http://code4node.com/snippet/http-proxy-with-custom-routing
这篇关于在Express中的路由定义中指定子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文