Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:44:52+00:00 2026-06-15T18:44:52+00:00

Is there a way to inject middleware in an Express stack? What I mean

  • 0

Is there a way to inject middleware in an Express stack? What I mean is I want to have my app.js setup the main middleware chain, and then call other modules passing the app instance and they may want to insert more middleware (e.g. an authentication module that wants to add passport in at the correct place)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:44:53+00:00Added an answer on June 15, 2026 at 6:44 pm

    You can certainly pass your app object to other modules and call use there. Of course, middleware functions are executed in the order they are added, so you have to take great care to ensure that you call use in the correct order.

    app.js

    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)
    

    mod.js

    exports.init = function(app) {
        app.use(function(req, res, next) {
    
        });
    };
    

    As far as actually injecting a middleware function in the middle of the stack (after you’ve already called app.use with a set of middleware functions), there’s no documented way to do it. use only adds a function to the end of the stack.

    use is actually supplied by Connect in proto.js:

    app.use = function(route, fn){
    
      ...
    
      this.stack.push({ route: route, handle: fn });
    
      return this;
    };
    

    Technically, you could fiddle with app.stack yourself, but I would not do this. You’d be messing with an undocumented implementation detail, which is liable to change. In other words, it’s possible a future update to either Connect or Express could break your app.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to inject R code output into the generated document without
Is there a way to inject EJB 3 stateless session beans into struts 2
Is there any way we can inject new methods and properties into classes during
Is there way to automatically maximize the output window on hitting build and then
Is there way to copy a file into Plone with WebDAV and have Plone
is there some simple way to inject Properties class loaded with a file from
Is there a way to inject the Unity container into an attribute in ASP.NET
is there a way to inject arbitrary code into a jar file? for example,
Is there any way to inject EJB3 / CDI / JSF beans (if the
I know there is a way to inject trusted certificates into the java runtime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.