question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apply middleware to all routes

See original GitHub issue

Is it possible to use middleware function for all routes?

In express.js you can quickly achieve this by applying function to the application’s use method:

app.use(function (req, res, next) {
 // do whatever you like
  next();
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
ddyrczcommented, Aug 18, 2017

I have configured it in the application module

@Module({
  modules: [
    CategoryModule,
    AuthModule
  ]
})
export default class ApplicationModule {
  configure(consumer: MiddlewaresConsumer) {
    consumer.apply(AuthMiddleware)
      .forRoutes({
        path: '*', method: RequestMethod.ALL
      });
  }
}

And it applies middleware function to all routes in the application

4reactions
adrien2pcommented, Aug 18, 2017

Hey ! yes of course, in the *.module.ts you can add the following things

export class UsersModule {
    configure(consumer: MiddlewaresConsumer) {
            consumer.apply(AuthMiddleware).forRoutes(YourController);
    }
}

that apply the middleware for all routes in the controller.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use specific middleware in Express for all paths except a ...
I would add checkUser middleware to all my paths, except homepage. app.get('/', routes.index); app.get('/account', checkUser, routes.account);.
Read more >
Using middleware - Express.js
Bind application-level middleware to an instance of the app object by using the app.use() and app.METHOD() functions, where METHOD is the HTTP method...
Read more >
Complete Guide to Express Middleware - Reflectoring
Application -level middleware which runs for all routes in an app object; Router level middleware which runs for all routes in a router ......
Read more >
Middleware - Laravel - The PHP Framework For Web Artisans
All of these middleware are located in the app/Http/Middleware directory. ... The application's route middleware groups.
Read more >
Build and Understand Express Middleware through Examples
Express middleware are functions that execute during the lifecycle of a request to the Express server. Each middleware has access to the HTTP ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found