How to connect express.js sub-applications?

I have several applications that I am trying to combine into one โ€œsetโ€: 2 applications are autonomous, one is simply an auth layer (using everyauth for FB Connect). I want to configure it like this:

  • / - (home) application list
  • / auth - login for any application
  • / app1 - requires login through / auth to access
  • / app2 - (same)

I considered leaving app1 and app2 autonomous, with the top layer being a proxy server, but I think it would be difficult to separate the auth system between them. Virtualhosts (via Connect) can work, but I donโ€™t necessarily need a DNS'd subdomain for each of them. Therefore, instead, I would like the main application to be an auth layer and the rest to be โ€œmountedโ€ into it, with the base path set in each application being subpath. (The base path is mentioned in the Guide expression, but not documented.)

They all use MongoDB, the auth level uses connect-mongodb for sessions, so I hope they can share the entire authentication / session level between them.

In another thread, How to exchange sessions in installed express applications , writes Stephen,

I have a rather complicated express application for web applications, which is divided into several auxiliary applications, which are also express applications (using app.use ()) ...

So how to use app.use() to mount a sub application? I was just trying to use var subApp = require('./subapp/app.js') , while listen() only works in a sub-application when ! module.parent ! module.parent (and not as a sub-application) ... but this seems to load all the sub-application paths directly into the parent application. I tried setting basepath with app.set('basepath', '/subapp/') , app.basepath = '/subapp/' , etc. Both in the sub-application itself and in the parent application, but this does not seem to have any effect.

Installing such applications makes it incredibly flexible, but it's unclear how to do it ... any advice would be greatly appreciated! (And I'm glad to share the lessons from my everyauth implementation if someone struggles with this.)

+30
Dec 15 '11 at 1:40
source share
2 answers

app.use(uri, instanceOfExpressServer)

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

An alternative is to use require("cluster") and call all of your applications on the same master so that they have the same port. Then just do the โ€œjust workโ€ routing

+38
Dec 15 '11 at 2:01
source share

Not sure if this will help you, but I was looking for a prefix for my API routes. I did that when I initialized the router, I added the mount path. So my configuration is as follows:

 //Default configuration app.configure(function(){ app.use(express.compress()); app.use(express.logger('dev')); app.set('json spaces',0); app.use(express.limit('2mb')); app.use(express.bodyParser()); app.use('/api', app.router); app.use(function(err, req, res, callback){ res.json(err.code, {}); }); }); 

Pay attention to "/ api" when calling the router

+6
Apr 19 '13 at 13:14
source share



All Articles