Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can certainly pass your <code>app</code> object to other modules and call <code>use</code> there. Of course, middleware functions are executed <strong>in the order they are added</strong>, so you have to take great care to ensure that you call <code>use</code> in the correct order.</p> <p>app.js</p> <pre><code>var app = express(); // ... app.use(express.logger()); // first middleware function var someOtherModule = require('./mod.js'); someOtherModule.init(app); app.use(express.static()); // last middleware function) </code></pre> <p>mod.js</p> <pre><code>exports.init = function(app) { app.use(function(req, res, next) { }); }; </code></pre> <hr> <p>As far as actually <em>injecting</em> a middleware function in the middle of the stack (after you've already called <code>app.use</code> with a set of middleware functions), there's no documented way to do it. <code>use</code> only adds a function to the end of the stack.</p> <p><code>use</code> is actually supplied by Connect in <a href="https://github.com/senchalabs/connect/blob/2.7.0/lib/proto.js#L62-93" rel="noreferrer">proto.js</a>:</p> <pre><code>app.use = function(route, fn){ ... this.stack.push({ route: route, handle: fn }); return this; }; </code></pre> <p><em>Technically</em>, you could fiddle with <code>app.stack</code> yourself, but <strong>I would not do this</strong>. You'd be messing with an undocumented implementation detail, which is liable to change. In other words, it's possible a future update to <em>either</em> Connect or Express could break your app.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload