nest icon indicating copy to clipboard operation
nest copied to clipboard

Apply middleware to all routes

Open ddyrcz opened this issue 9 years ago • 12 comments

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();
});

ddyrcz avatar Aug 18 '17 07:08 ddyrcz

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.

adrien2p avatar Aug 18 '17 07:08 adrien2p

...that apply the middleware for all routes in the controller.

You need to list the controllers that you want to apply middleware function to (if you add new controller it need to be added to that list)

But is there a way to apply middleware function on all the routes of every controller in nest app?

ddyrcz avatar Aug 18 '17 08:08 ddyrcz

Ok sorry for the miss understanding, in this case you can use instance.all('*', YourMiddleWare) That's apply it on all incoming route path whatever the method used(GET, POST, PUT, ...)

const instance = express();
instance.all('*', MiddleWare);
const app = NestFactory.create(ApplicationModule, instance);

adrien2p avatar Aug 18 '17 08:08 adrien2p

So as I can see it can be achieved using express instance outside nest app

thanks @adrien2p

ddyrcz avatar Aug 18 '17 08:08 ddyrcz

@adrien2p

I have not noticed this in the documentation. You can also achieve this by defining the path for which the middleware function should by applied like this:

consumer.apply(AuthMiddleware).forRoutes({ 
    path: '*', method: RequestMethod.ALL 
});

ddyrcz avatar Aug 18 '17 08:08 ddyrcz

Yes you can do this but i dont know if it's apply in all route app, may be in the applicationModule that could works

adrien2p avatar Aug 18 '17 08:08 adrien2p

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

ddyrcz avatar Aug 18 '17 09:08 ddyrcz

Is it possible to run middleware for all routes except for one? For example, I want to run authorization on all routes except for the one that handles the initial authentication request.

adammfrank avatar Oct 21 '17 16:10 adammfrank

@adammfrank this same was recommended in #17 since April check this, I think this would be very useful

cdiaz avatar Oct 22 '17 14:10 cdiaz

public resolve() { return async (req, res, next) => { if (req.headers.authorization ){ const token = req.headers.authorization; const decoded: any =passport.authenticate(token, process.env.JWT_KEY || ''); console.log(decoded)

};
next();

} }

riadhriadh avatar Oct 21 '18 18:10 riadhriadh

` public resolve() { return async (req, res, next) => { if (req.headers.authorization ){ const token = req.headers.authorization; const decoded: any =passport.authenticate(token, process.env.JWT_KEY || ''); console.log(decoded)

};
next();

}`

riadhriadh avatar Oct 21 '18 18:10 riadhriadh

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 24 '19 11:09 lock[bot]