Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nest Router with Middleware not work #5

Closed
WClouds opened this issue Feb 26, 2018 · 3 comments
Closed

Nest Router with Middleware not work #5

WClouds opened this issue Feb 26, 2018 · 3 comments

Comments

@WClouds
Copy link

WClouds commented Feb 26, 2018

If I use nest-router, when I add a logger middleware for ApplicationModule, the middleware not work.
But when I remove nest-router, middleware worked. Is there any conflicts between nest-router and middleware?

// app.module.ts
import { Module, NestModule, MiddlewaresConsumer, RequestMethod } from '@nestjs/common';
import { RouterModule } from 'nest-router';
import { routes } from './routes';
import { SoftwareModule } from './software/software.module';
import { loggerMiddleware } from './logger.middleware';

@Module({
  imports: [RouterModule.forRoutes(routes), SoftwareModule],
})
export class ApplicationModule implements NestModule {
  configure(consumer: MiddlewaresConsumer): void {
    consumer
      .apply(loggerMiddleware)
      .forRoutes({
        path: '/api/v2',
        method: RequestMethod.ALL
      })
  }
}
// logger.middleware.ts
export const loggerMiddleware = (req, res, next) => {
  console.log(`Request...`);
  next();
};
// routes.ts
import { Routes } from 'nest-router';
import { SoftwareModule } from './software/software.module';

const v2_modules = [
  SoftwareModule,
]

const route = v2_modules.map(module => {
  return {
    path: '/api/v2',
    module,
  }
})

export const routes: Routes = route;
@shekohex
Copy link
Member

Hi @WClouds
Could you share a Minimal reproduction of the problem, so I can solve it.
but I just tested the simple loggerMiddleware with the example provided with Nest Router Repo, and it just works fine.

@Metheny80
Copy link

Metheny80 commented Apr 2, 2021

@shekohex I still can't get it to work.
The example in the repo sets up the middleware for the other modules in the main App Module, but in my case I have a few modules, and each sets up its own middleware.

For example, in the main App Module I set up the routes:

// app-module.ts

const routes: Routes = [
  {
    path: '/my',
    module: MyModule,
  },
];

@Module({
  imports: [
    RouterModule.forRoutes(routes)
 ])
export class AppModule { }

However when I do the following in MyModule, the middlewares are not actually being set up (called) for the module's routes:

// my-module.ts
consumer.apply(MyMiddleware).forRoutes(MyController);

// my-controller.ts

@Controller('path')
export class MyController {
  @Get()
  async action(): Promise<any> {
    return;
 }

Calling GET my/path doesn't trigger MyMiddleware.

I tried moving the routes definition to MyModule, but that doesn't work either.
When setting up the middleware like above, should the consumer take into account the prefix set in the AppModule?
Or any other idea why this doesn't work?

@shekohex
Copy link
Member

shekohex commented Apr 2, 2021

@Metheny80 please see the README.

but you should instead use this code

​consumer​.​apply​(​someMiddleware​)​.​forRoutes​(​RouterModule​.​resolvePath​(​SomeController​)​)​;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants