如何为Node.js应用程序设置本地子域 [英] How to set up local subdomains for Node.js app
问题描述
127.0.0.1 localhost sub1.localhost sub2.localhost
。虽然这可以让我将一个子域添加到本地主机,但是该模块不会将此识别为有效的子域,因此在我的根路径中搜索子域路由。 main.js:
var routes = require('./ routes / index')(护照)
var sub1_routes = require('./ routes / sub1')(护照);
var sub2_routes = require('./ routes / sub2')(护照);
app.use(subdomain('sub1',sub1_routes));
app.use(subdomain('sub2',sub1_routes));
app.use('/',routes);
我需要能够在本地处理。
我是作者模块:)
对于您希望在本地测试的每个新子域,您必须添加到/ etc / hosts文件中。所以例如:
localhost是:
127.0.0.1 localhost
一个新的子域将是..
127.0.0.1 sub1.localhost
另一个..
127.0.0.1 sub2.localhost
查看我在测试中所做的工作。 p>
I am running an express app on node.js. The app uses the express-subdomain module to help handle routes for two different subdomains (sub1.example.com and sub2.example.com). I'm hosting the app on AWS Elastic Beanstalk. In my production environment, everything works great. But on my local machine, I cannot get this to work. I tried adding the subdomains to my host file 127.0.0.1 localhost sub1.localhost sub2.localhost
. Although that allows me to prepend a subdomain to localhost, the module doesn't recognize this as a valid subdomain, and therefor searches for subdomain routes in my root routes.
In main.js:
var routes = require('./routes/index')(passport);
var sub1_routes = require('./routes/sub1')(passport);
var sub2_routes = require('./routes/sub2')(passport);
app.use(subdomain('sub1', sub1_routes));
app.use(subdomain('sub2', sub1_routes));
app.use('/', routes);
I need to be able to handle this locally. It takes to much time to push a small change to AWS test, iterate, etc.
I'm the author of the module :)
For each new subdomain you wish to test locally you must add into your /etc/hosts file. So for example:
localhost is:
127.0.0.1 localhost
a new subdomain would be..
127.0.0.1 sub1.localhost
and another..
127.0.0.1 sub2.localhost
Check out what I have done in the tests.
这篇关于如何为Node.js应用程序设置本地子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!