question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware for all routes not working with pathless controller and methods

See original GitHub issue

Bug Report

Current behavior

A Middleware configured with forRoutes('*') is not called with a controller without path, such like @Controller(), and also a method without any path, like @Get(). It works with a path in either of them however. The reason behind a pathless controller is I already have a app.setGlobalPrefix('api/...') in my app and I am putting up a controller for that path of global prefix, so no need for a path.

Input Code

//Basically like
NestFactory.create(UserAPIModule, adapter)
  .then(app => {
    app.setGlobalPrefix('api/user');
    app.enableCors();
    return app.init();
  })

// Then...

@Module({
  imports: [UserModule],
})
export class UserAPIModule {
  public configure(consumer: MiddlewareConsumer) {
    consumer.apply(MyMiddleware).forRoutes('*');
  }
}

@Injectable()
export class MyMiddleware implements NestMiddleware {
  public use(req: Request, res: Response, next: Function) {
    console.log('THIS IS NOT CALLED');
    next();
  }
}

@Module({
  imports: []
  controllers: [UserController],
})
export class UserModule {}


@Controller()
export class UserController {
  constructor() {}

  @Get()
  public async getUser() {
    console.log('this is called without middlware');
    return 'Users object';
  }
}

Expected behavior

Middlewares should also be called on pathless controllers due to the meaning of the wildcard

Possible Solution

Environment


Nest version: 6.2.4

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
soumyartcommented, Jun 26, 2019

I’m also facing the same issue. In my use case if you put a trailing slash (http://localhost:3000/api/user/) while accessing the api then middleware is invoked but if you remove the trailing slash (http://localhost:3000/api/user) it is bypassed.

0reactions
kamilmysliwieccommented, Apr 10, 2020

This has been clarified in the documentation (as part of the v7 release) https://docs.nestjs.com/middleware#route-wildcards

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nestjs: middleware for all routes except /auth - Stack Overflow
In the documentation it says that I can add all the controllers or paths of the routes where I want the middleware, but...
Read more >
React-router nested pathless routes as middleware : r/reactjs
Is it good practice to use multiple nested patheless routes (returning Outlet or Navigate depending on logic) to act as middleware ?
Read more >
Middleware | NestJS - A progressive Node.js framework
Middleware is a function which is called before the route handler. Middleware functions have access to the request and response objects, and the...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
Router middleware as it allows us to group the route handlers for a particular part of a site together and access them using...
Read more >
Match - React Router: Declarative Routing for React.js
A match object contains information about how a <Route path> matched the URL. match ... If a Route does not have a path...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found