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

  • SEARCH
  • Home
  • 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 6530157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:43:17+00:00 2026-05-25T09:43:17+00:00

Almost every Express app I see has an app.use statement for middleware but I

  • 0

Almost every Express app I see has an app.use statement for middleware but I haven’t found a clear, concise explanation of what middleware actually is and what the app.use statement is doing. Even the express docs themselves are a bit vague on this. Can you explain these concepts for me please?

  • 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-05-25T09:43:17+00:00Added an answer on May 25, 2026 at 9:43 am

    middleware

    I’m halfway through separating the concept of middleware in a new project.

    Middleware allows you to define a stack of actions that you should flow through. Express servers themselves are a stack of middlewares.

    // express
    var app = express();
    // middleware
    var stack = middleware();
    

    Then you can add layers to the middleware stack by calling .use

    // express
    app.use(express.static(..));
    // middleware
    stack.use(function(data, next) {
      next();
    });
    

    A layer in the middleware stack is a function, which takes n parameters (2 for express, req & res) and a next function.

    Middleware expects the layer to do some computation, augment the parameters and then call next.

    A stack doesn’t do anything unless you handle it. Express will handle the stack every time an incoming HTTP request is caught on the server. With middleware you handle the stack manually.

    // express, you need to do nothing
    // middleware
    stack.handle(someData);
    

    A more complete example :

    var middleware = require("../src/middleware.js");
    
    var stack = middleware(function(data, next) {
        data.foo = data.data*2;
        next();
    }, function(data, next) {
        setTimeout(function() {
            data.async = true;
            next();
        }, 100)
    }, function(data) {
        console.log(data);
    });
    
    stack.handle({
        "data": 42
    })
    

    In express terms you just define a stack of operations you want express to handle for every incoming HTTP request.

    In terms of express (rather than connect) you have global middleware and route specific middleware. This means you can attach a middleware stack to every incoming HTTP requests or only attach it to HTTP requests that interact with a certain route.

    Advanced examples of express & middleware :

    // middleware 
    
    var stack = middleware(function(req, res, next) {
        users.getAll(function(err, users) {
            if (err) next(err);
            req.users = users;
            next();  
        });
    }, function(req, res, next) {
        posts.getAll(function(err, posts) {
            if (err) next(err);
            req.posts = posts;
            next();
        })
    }, function(req, res, next) {
        req.posts.forEach(function(post) {
            post.user = req.users[post.userId];
        });
    
        res.render("blog/posts", {
            "posts": req.posts
        });
    });
    
    var app = express.createServer();
    
    app.get("/posts", function(req, res) {
       stack.handle(req, res); 
    });
    
    // express
    
    var app = express.createServer();
    
    app.get("/posts", [
        function(req, res, next) {
            users.getAll(function(err, users) {
                if (err) next(err);
                req.users = users;
                next();  
            });
        }, function(req, res, next) {
            posts.getAll(function(err, posts) {
                if (err) next(err);
                req.posts = posts;
                next();
            })
        }, function(req, res, next) {
            req.posts.forEach(function(post) {
                post.user = req.users[post.userId];
            });
    
            res.render("blog/posts", {
                "posts": req.posts
            });
        }
    ], function(req, res) {
       stack.handle(req, res); 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seem to be examples for TreeMaps for almost every language but PHP. Has
Almost every Python web framework has a simple server that runs a wsgi application
Almost every flash player has an option to display how much of buffer (or
I used Term::ShellUI and almost every thing is working as expected but the issue
Every place I've worked in the past 15 years has had, on almost every
Almost every game use keyboard as input. I have been searching for 2 days
The IEEE has a long list of standards for almost every step within the
i'm flabbergasted, i've looked at almost every example, but it just doesn't work (the
In my Database almost every table has its own translations table. I.e. Sports has
I see this word in almost every cross service application these days. What exactly

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.