1
\$\begingroup\$

I am currently working on an Node based Express application. I have found Express's middlewares approach quite pleasing however, we all run in scenarios where a certain route or routes don't need a specific middleware.

I have found two approaches:

  1. Adding middleware globally exempting the routes it doesn't need to run for. express-jwt is good example of this:

    app.use(jwt().unless(
        '/someroute',
        '/someotherroute',
        ...
    );
    
    app.use(someothermiddleware().unless(
        '/someroute2',
        ...
    );
    
    app.use(someothermiddlewaretoo().unless(
        '/someroute3'
        ...
    );
    
  2. Adding middleware(s) to each route you define:

    route.get(
       '/someroute1',
       [ jwt(), someothermiddleware, someothermiddlewaretoo ],
       function(req, res) {
           ...
       }
    );
    
    route.get(
       '/someroute2',
       [ jwt(), someothermiddlewaretoo ],
       function(req, res) {
           ...
       }
    );
    
    route.get(
       '/someroute3',
       [ jwt(), someothermiddleware ],
       function(req, res) {
           ...
       }
    );
    

I can see that Approach #2 looks like some work however, I believe Approach #2 is way better in terms of a large application where you might have nested routers. You can be on any route and know exactly what middlewares it gets executed with.

With Approach #1, you get things up and running easily but, it will be pain to check if this route is being included for specific middleware every time you try to fix a bug.

Which approach is better in your opinion? Or, is there another approach that you would like to share?

\$\endgroup\$
2
\$\begingroup\$

I personally prefer Approach 2 since it will increase readability and you can easily find out which middle-ware are applied to a route rather than to go to each excepted middle-ware and calculate. That is a lot of work for future purpose.

\$\endgroup\$
  • \$\begingroup\$ Agreed. Approach #2 seems better. But, I would advise creating middleware groups and apply groups to the routes because, if you need to apply another middleware, you can add it to middleware group and avoid having to put it on per-route basis. \$\endgroup\$ – Rex Overflow Aug 8 at 9:42
  • \$\begingroup\$ For clarity, assume you have middlewares folder and within it, all middlewares as separate files and an index.js files that imports those middlewares and export them as a single object. In addition to exporting the middlewares individually, you can create an array of middlewares as a group (say auth group) and apply that group to routes exempting routes that need special treatment. You can apply some specific middleware or other group to those special routes. In this way, if you need to add another middleware when user is authenticated, you can update just auth group array. \$\endgroup\$ – Rex Overflow Aug 8 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.