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 8615223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:19:19+00:00 2026-06-12T05:19:19+00:00

Before I ask about app.router I think I should explain at least what I

  • 0

Before I ask about app.router I think I should explain at least what I think happens when working with middleware. To use middleware, the function to use is app.use(). When the middleware is being executed, it will either call the next middleware by using next() or make it so no more middleware get called. That means that the order in which I place my middleware calls is important, because some middleware depends on other middleware, and some middleware near the end might not even be called.

Today I was working on my application and had my server running in the background. I wanted to make some changes and refresh my page and see the changes immediately. Specifically, I was making changes to my layout. I couldn’t get it to work so I searched Stack Overflow for the answer and found this question. It says to make sure that express.static() is beneath require('stylus'). But when I was looking at that OP’s code, I saw that he had his app.router call at the very end of his middleware calls, and I tried to figure out why that was.

When I made my Express.js application (version 3.0.0rc4), I used the command express app --sessions --css stylus and in my app.js file the code came setup with my app.router above both the express.static() and require('stylus') calls. So it seems like, if it comes already setup that way, then it should stay that way.

After re-arranging my code so I could see my Stylus changes, it looks like this:

app.configure(function(){
  //app.set() calls
  //app.use() calls
  //...
  app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(express.static(__dirname + '/public', {maxAge: 31557600000}));
});

app.get('/', routes.index);

app.get('/test', function(req, res){
  res.send('Test');
});

So I decided that the first step would be to find out why it is important to even have app.router in my code. So I commented it out, started my app and navigated to /. It displayed my index page just fine. Hmm, maybe it worked because I was exporting the routing from my routes file (routes.index). So next I navigated to /test and it displayed Test on the screen. Haha, OK, I have no idea what app.router does. Whether it is included in my code or not, my routing is fine. So I am definitely missing something.

So Here Is My Question:

Could somebody please explain what app.router does, the importance of it, and where I should place it in my middleware calls? It would also be nice if I got a brief explanation about express.static(). As far as I can tell, express.static() is a cache of my information, and if the application can’t find the requested page, it will check the cache to see if it exists.

  • 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-12T05:19:20+00:00Added an answer on June 12, 2026 at 5:19 am

    Note: This describes how Express worked in versions 2 and 3. See the end of this post for information about Express 4.


    static simply serves files (static resources) from disk. You give it a path (sometimes called the mount point), and it serves the files in that folder.

    For example, express.static('/var/www') would serve the files in that folder. So a request to your Node server for http://server/file.html would serve /var/www/file.html.

    router is code that runs your routes. When you do app.get('/user', function(req, res) { ... });, it is the router that actually invokes the callback function to process the request.

    The order that you pass things to app.use determines the order in which each middleware is given the opportunity to process a request. For example, if you have a file called test.html in your static folder and a route:

    app.get('/test.html', function(req, res) {
        res.send('Hello from route handler');
    });
    

    Which one gets sent to a client requesting http://server/test.html? Whichever middleware is given to use first.

    If you do this:

    app.use(express.static(__dirname + '/public'));
    app.use(app.router);
    

    Then the file on disk is served.

    If you do it the other way,

    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
    

    Then the route handler gets the request, and "Hello from route handler" gets sent to the browser.

    Usually, you want to put the router above the static middleware so that a accidentally-named file can’t override one of your routes.

    Note that if you don’t explicitly use the router, it is implicitly added by Express at the point you define a route (which is why your routes still worked even though you commented out app.use(app.router)).


    A commenter has brought up another point about the order of static and router that I hadn’t addressed: the impact on your app’s overall performance.

    Another reason to use router above static is to optimize performance. If you put static first, then you’ll hit the hard drive on every single request to see whether or not a file exists. In a quick test, I found that this overhead amounted to ~1ms on an unloaded server. (That number is much likely to be higher under load, where requests will compete for disk access.)

    With router first, a request matching a route never has to hit the disk, saving precious milliseconds.

    Of course, there are ways to mitigate static‘s overhead.

    The best option is to put all of your static resources under a specific folder. (IE /static) You can then mount static to that path so that it only runs when the path starts with /static:

    app.use('/static', express.static(__dirname + '/static'));
    

    In this situation, you’d put this above router. This avoids processing other middleware/the router if a file is present, but to be honest, I doubt you’ll gain that much.

    You could also use staticCache, which caches static resources in-memory so that you don’t have to hit the disk for commonly requested files. (Warning: staticCache will apparently be removed in the future.)

    However, I don’t think staticCache caches negative answers (when a file does not exist), so it doesn’t help if you’ve put staticCache above router without mounting it to a path.

    As with all questions about performance, measure and benchmark your real-world app (under load) to see where the bottlenecks really are.


    Express 4

    Express 4.0 removes app.router. All middleware (app.use) and routes (app.get et al) are now processed in precisely the order in which they are added.

    In other words:

    All routing methods will be added in the order in which they appear. You should not do app.use(app.router). This eliminates the most common issue with Express.

    In other words, mixing app.use() and app[VERB]() will work exactly in the order in which they are called.

    app.get('/', home);
    app.use('/public', require('st')(process.cwd()));
    app.get('/users', users.list);
    app.post('/users', users.create);
    

    Read more about changes in Express 4.

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

Sidebar

Related Questions

I read a lot of documentation about this before i decided to ask. So
I tried to ask this question before but I guess I didnt explain myself
I want to ask why we use this keyword before the parameter in an
Before I ask my question, I want to mention that I am only today
Before I ask the question let me state that I have attempted to google
Before I ask this question I would to apologise because of the fact that
Before I ask this, do note: I want this for debugging purposes. I know
I don't know if this question has been ask before. But I have a
The SO community was right, profiling your code before you ask performance questions seems
Before you guys ask, yes I've searched online for the answer, but everything I

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.