如何挂载express.js子应用程序? [英] How to mount express.js sub-apps?

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

问题描述

我要尝试将多个应用程序合并为一个套件":2个应用程序是独立的,一个只是一个auth层(对FB Connect使用everyauth).我想像这样设置它:

I have several apps that I'm trying to merge into a single "suite": 2 apps are standalone, one is simply an auth layer (using everyauth for FB Connect). I want to set it up like so:

  • /-(首页)应用列表
  • /auth-登录任何应用程序
  • /app1-需要通过/auth登录才能访问
  • /app2-(相同)

我曾考虑过让app1和app2独立,而顶层是代理,但是我认为要在它们之间共享身份验证系统很困难. 虚拟主机(通过Connect)也许可以工作,但我不一定要为每个主机都使用一个DNS子域. 因此,我改为将主要应用程序作为auth层,将其他应用程序装入"到该层中,并在每个应用程序上将 basepath 设置为子路径. (在指南中提到了基本路径,但没有很好地记录.)

I've considered leaving app1 and app2 standalone, with the top layer being a proxy, but I think it would be hard to share an auth system between them. Virtualhosts (via Connect) might work, but I don't necessarily want a DNS'd subdomain for each one. So instead I'd like to primary app to be the auth layer, and the others "mounted" into that one, with basepath set on each app to a sub-path. (basepath is mentioned in the express Guide but not documented well.)

他们都使用MongoDB,auth层使用connect-mongodb进行会话,所以我希望他们能够在它们之间共享整个auth/session层.

They all use MongoDB, the auth layer uses connect-mongodb for sessions, so I'm hoping they'll be able to share the whole auth/session layer between them.

在另一个线程中,如何在已安装的Express应用程序中共享会话",斯蒂芬写道,

In another thread, "How to share sessions in mounted express apps", Stephen writes,

我有一个基于Express的相当复杂的Web应用程序,它分为几个子应用程序,这些子应用程序也都是Express应用程序(使用app.use())...

I have a fairly complex express based web application that is split up into a few sub apps which are also express apps (using app.use()) ...

那么如何使用app.use()挂载子应用程序?我只是尝试使用var subApp = require('./subapp/app.js'),而listen()仅在! module.parent时在子应用程序中运行(因此不能作为子应用程序运行)...但这似乎会将所有子应用程序的路径直接加载到父应用.我尝试在子应用程序本身和父应用程序中都使用app.set('basepath', '/subapp/')app.basepath = '/subapp/'等设置basepath,但这似乎没有任何效果.

So how does one use app.use() to mount a sub-app? I was simply trying to use var subApp = require('./subapp/app.js'), with listen() running only in the sub-app when ! module.parent (so not as a sub-app)... but that seems to load all the sub-app's paths straight into the parent app. I've tried setting basepath using app.set('basepath', '/subapp/'), app.basepath = '/subapp/', etc, both in the sub-app itself and from the parent app, but it doesn't seem to have any effect.

这样的安装应用程序使快递表达了难以置信的灵活性,但是目前尚不清楚该怎么做...任何建议都将非常受欢迎! (如果有人为此感到苦恼,我很乐意分享我的Everyauth实现的经验教训.)

Mounting apps like this makes express incredibly flexible, but it's not clear how to do it... any advice would be extremely welcome!! (And I'm happy to share lessons from my everyauth implementation if anyone is struggling with that.)

推荐答案

app.use(uri, instanceOfExpressServer)

只需确保您没有在其上拨打.listen.

Just make sure you don't call .listen on it.

另一种方法是使用require("cluster")并在一个主服务器中调用所有应用程序,以便它们共享相同的端口.然后只需路由即可正常工作"

The alternative is to use require("cluster") and invoke all your apps in a single master so that they share the same port. Then just get the routing to "just work"

这篇关于如何挂载express.js子应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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