Questions & Answers

Can I limit middleware execution to specific controllers or routes?

Deepkit HTTP
50 up-votes
Warning: The answer is generated by an artificial intelligence. It might not be correct.
To adjust rating, open the thread in Discord and click on the up/down vote button.

Yes, you can limit the execution of middleware to specific controllers or routes in Deepkit HTTP.

To apply middleware to specific controllers or routes, you can use the httpMiddleware.for(T).forControllers() or httpMiddleware.for(T).forRoutes() methods, where T is the middleware class or function.

For example, to apply a middleware to specific controllers, you can use the forControllers() method:

import { httpMiddleware } from '@deepkit/http';

// ...

new App({
  // ...
  middlewares: [
    httpMiddleware.for(MyMiddleware).forControllers(MyController1, MyController2),
  ],
  // ...
}).run();

Alternatively, you can apply middleware to specific routes using the forRoutes() method:

import { httpMiddleware } from '@deepkit/http';

// ...

new App({
  // ...
  middlewares: [
    httpMiddleware.for(MyMiddleware).forRoutes({
      path: '/route1',
      httpMethod: 'GET',
    }),
  ],
  // ...
}).run();