P
P
Pavel2019-11-12 12:15:16
Node.js
Pavel, 2019-11-12 12:15:16

How to add 2 different intermediaries to different routes in NestJS inside one module?

The documentation only describes how to add multiple alternate middleware to the same routes https://docs.nestjs.com/middleware#multiple-middleware

But I need to add 2 middleware to 2 independent routes from 2 controllers.

Now I have this code

export class ImageModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer
            .apply(AwsImagesMiddleware)
            .forRoutes('dashboard/images/upload-images');
    }
}


You need to add AwsAvatarsMiddleware here for '/images/upload-avatar'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2019-11-12
@Palych_tw

Yes, everything was easier. The forRoutes method returns the consumer again.
So just like this

export class ImageModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer
            .apply(AwsImagesMiddleware)
            .forRoutes('dashboard/images/upload-images')
            .apply(AwsAvatarsMiddleware)
            .forRoutes('images/upload-avatar');
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question