Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How does mounting apps in Express work

How does mounting apps in Express work

Problem

So I've seen TJ's guide to creating modular Express-apps, followed it to good effects, but want to know more about the details of how it works, however a search gives me no answers.

In short I am wondering: When mounting apps in Express, what parts of the apps are shared and what parts are not?

Some examples to clarify my question:

app.js:

app.use(express.bodyParser()); 

app.use(loginApi); //loginApi is an express app 

app.listen(3000);

This example works. But if I place the app.use(loginApi) before app.use(express.bodyParser()); , the body parser will not be available in the loginApi subapp. Why is that?

Another example:

submodule.js

var app = module.exports = require('express')();
app.all('*', function(req, res, next){
    console.log('nifty middleware');
        next();
});

app.js

app.get('/funtimes', fn);
app.use(submodule); 
app.listen(3000);

Now in this example, If I understand it correctly, the /funtimes route will not be affected by the submodule middleware for all routes. But what about the rest of the routes of app.js ? Will they be affected? And what if I add another module, will it be affected?

Problem courtesy of: Soroush Hakami

Solution

if I place the app.use(loginApi) before app.use(express.bodyParser()); , the body parser will not be available in the loginApi subapp. Why is that?

That's because of the way Express handles requests. Any incoming Request starts at the top of the middleware stack, starting with app.use() stack.

Middleware are simply functions that have the Function signature function(req, res, next) which either call next() if they want to hand off the request to subsequent functions, or send a response themselves. You define a 'middleware chain' of a bunch of these functions (many are provided by express, like express.logger() and express.compress().)

So in the following scenario:

app.use(express.bodyParser());
var loginApi = require('./api/index.js')
app.use(loginApi);
app.use(app.router);

then an incoming request will first hit app.use(express.bodyParser()), parsing req.body. Then that function calls its internal next(), passing it to the next function in the middleware chain (app.use(loginApi)). The loginApi app has its own middleware chain, but the requests already have req.body set from the outer app. If the loginApi doesn't send a response, the request will continue to app.use(app.router) and at that point the requests will go to the routing functions you set up in your outer app.

So the answer is: A mounted app will have the middleware functions shared, which are placed before the line app.use(loginApi)

Solution courtesy of: Plato

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How does mounting apps in Express work

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×