如何为Node.js应用程序设置本地子域 [英] How to set up local subdomains for Node.js app

查看:110
本文介绍了如何为Node.js应用程序设置本地子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在node.js上运行一个快速应用程序。该应用程序使用 express-subdomain模块来帮助处理两个不同子域(sub1)的路由。 example.com和sub2.example.com)。我在AWS Elastic Beanstalk上托管该应用程序。在我的生产环境中,一切都很好。但是在我的本地机器上,我无法让它工作。我尝试将子域添加到我的主机文件 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆