如何使用express.js的req.subdomains获取本地子域名?

时间:2015-03-19 13:54:32

标签: node.js express

Here is my /etc/hosts file

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
127.0.0.1   test.localhost
127.0.0.1   demo.localhost
127.0.0.1   test2.localhost
255.255.255.255 broadcasthost
::1             localhost 

我需要的是能够从url中解析子域,因为我必须将它存储在会话cookie中以供会话使用(现在)。我没有尝试做任何奇特的事情 - 比如现在设置api或任何东西。

Express的文档显示,有一个req.subdomains属性将子域列为数组,但是对于所有本地子域,此数组显示为空。 [即当我去demo.localhost:3000时,Web应用程序肯定会提供,但在第一个get函数中,我调用req.subdomains并且有一个空数组[]。

如何从localhost:3000获取这些子域名字符串?

1 个答案:

答案 0 :(得分:12)

这是因为您的主机缺少TLD。 From the Express code - 子域偏移量默认为2,因为它需要.com而不是主机以localhost结尾。

您可以使用this.app.set('subdomain offset', 1);作为本地运行配置的一部分。