#Middleware with parameter

12 messages · Page 1 of 1 (latest)

craggy agate
#

I want to pass some paramteres in middleware how can implement this?

rough carbon
#

What kind of parameter? What do you mean?

craggy agate
#

I want to send some values in middleware, like a number or string when I will use it on module

rough carbon
#

Can you show a pseudocode example?

craggy agate
#

In my express project I am doing this, and I am migrating the whole code in nestjs

#

so in userTypeMiddleware im passing some extra values

#

so that thing I want to do with nest middleware

rough carbon
#

Why not something like:

@Injectable()
export class MyMiddleware {
  use(numArray: number[]) {
    return (req, res, next) => {
      // do the middleware with access to numArray due to the closure
    }
  }
}
#

Then use it as consumer.use(new MyMiddleware().use(numArray))

craggy agate
#

Okay

#

Thank you @rough carbon ❤️