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 · 3 comments
Closed

Middleware runs more than once #779

rmvermeulen opened this issue Jun 11, 2018 · 3 comments

Comments

@rmvermeulen
Copy link

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
Copy link
Contributor

cojack commented Jun 26, 2018

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

@kamilmysliwiec
Copy link
Member

Fixed in the v5.1.0

@lock
Copy link

lock bot commented Sep 24, 2019

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 lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants