Two Express "applications on the same NodeJS server?

I want to be able to send different requests through different middleware stacks.

I think I need two app s expressions, and some kind of branching. eg:.

 var app = express(); var alt_app = express(); app.use(logger('dev')); app.use(bodyParser.json()); app.use(function(req, res, next) { if (some_condition(req)) { divert_request_to_alt_app(); } else { next(); } } app.use(middleware_that_must_not_affect_alt_app()); alt_app.use(middleware_that_only_affects_alt_app()); 

Is such a strategy possible?

If so, what should divert_request_to_alt_app() be?

If not, what other approaches?

+5
source share
1 answer

I found that express applications are functions. A call using (req, res, next) is all you need to do to transfer a request from one application to another.

In the example below, I have one root app app and two branches app0 and app1 .

 var app0 = require('./app0'); var app1 = require('./app1'); var app = express(); ... app.use(function(req, res, next) { if (some_condition_on(req)) { app0(req, res, next); } else { app1(req, res, next); } }); 
+3
source

All Articles