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

Middleware runs more than once #779

Closed
rmvermeulen opened this issue Jun 11, 2018 · 2 comments

Comments

@rmvermeulen
Copy link

commented Jun 11, 2018

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Logger middleware called more than once per request

Expected behavior

Middleware is called once for a request

Minimal reproduction of the problem with instructions

Using the latest cats sample, with the cats.module.ts applying the logger middleware

// logger middleware
@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  resolve(context: string): MiddlewareFunction {
    return (req, res, next) => {
      console.log(`[${context}] [${req.method}] [${req.baseUrl}] Request...`);
      next();
    };
  }
}
// cats module
export class CatsModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware)
      .with('CatsModule')
      .forRoutes(CatsController);
  }
}

After curl http://localhost:3000/cats/12 the server outputs

[CatsModule] [GET] [/cats] Request...
[CatsModule] [GET] [/cats/12] Request...
Before...
After... 0ms

Note that if I use .forRoutes('/cats') (instead of the controller class), or apply the middleware in main.ts with app.use(...), I get the expected behaviour.

Environment


Nest version: 5.0.1

 
For Tooling issues:
- Node version: 8.9.4  
- Platform: Mac 

Others:

@cojack

This comment has been minimized.

Copy link

commented Jun 26, 2018

@kamilmysliwiec could you point issues in commits? It will be nice hooked together ;)

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented Jul 5, 2018

Fixed in the v5.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.