#Middleware error

1 messages · Page 1 of 1 (latest)

noble spruce
#

Hey, when using the example middleware from nestjs:

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    console.log('Request...');
    next();
  }
}

and registering it in the main.ts like this:

app.use(LoggerMiddleware)

I get this error:
Class constructor LoggerMiddleware cannot be invoked without 'new'

I found this github issue https://github.com/nestjs/nest/issues/4467 but well they say use discord your code is wrong, but not what exactly is wrong..

GitHub

Bug Report Current behavior I create an empty middleware class according to documentation. If I register it using app.use(), I get an exception "TypeError: Class constructor LoggerMiddleware c...

frosty cipher
#

You can't app.use(ClassReference) you'd need to do app.use(new LoggerMiddleware()) (I believe that would be fine, you may need to do it in two steps to create the instance then pass use and bind the instanceto use)
Or if you want to be able to just reference the class, you need to use a class the implements NestModule and has a configure method where you canpass a MiddlewareConsumer that can do consumer.apply(ClassReference) as shown in the docs

noble spruce
#

Btw, when I save an object or variable inside of the AppModule on request, is it possible that it will be overwritten when another request comes in while the other is still processing?

frosty cipher
noble spruce
#

In a middleware I set an object into let’s call it test so AppModule.test = …

I use that in a custom httpexception to do stuff.

frosty cipher
#

That can and will be overwritten by subsequent requests

noble spruce
#

Okay, what’s the best way to do that then?

frosty cipher
#

Set it as a part of the request object. req.test = stuff

noble spruce
#

But I also use it in a service file, can I access the request in that?

frosty cipher
#

Pass the request from the controller or inject it by making the service REQUEST scoped

noble spruce
#

Like this:
@Injectable({scope: Scope.REQUEST})?

frosty cipher
#

And the @Inject(REQUEST), just be aware there's a performance hit for it

noble spruce
#

Okay, but my variable doesn’t exist on Request

this.request.test <- test doesn’t exist on Request

frosty cipher
#

Where are you trying to read it?

noble spruce
frosty cipher
#

Don't read it in the constructor. That will try to read it before the middleware has had a chance to run

noble spruce
#

thats in the middleware, how do you mean that i should do it in the service?

frosty cipher
#

That's fine in the middleware. Just don't access/read it in the constructor. Do it in the method where you need it

noble spruce
#

but how do i access the request in the method then?

frosty cipher
#

this.request is fine. I'm just telling you to not access the test property of this.request until you need it in the method

noble spruce
#

I have now:

@Injectable({scope: Scope.REQUEST})
export class TestService{
constructor(@Inject(REQUEST) private request: Request){ }
  async getTest(){
    console.log(this.request.test); // <- says test doesnt exist
  }
}
frosty cipher
#

You're using the express adapter, right? And you're certain the middleware has ran?

noble spruce
#

yes, "Request" is imported from "express", middleware has ran

frosty cipher
#

Oh, that's a Typescript error because it doesn't know you added custom properties

noble spruce
#

yess

frosty cipher
#

So make a custom type. Or just call it Request & { test: string } or whatever its type is