FoalTS / foal

Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
https://foalts.org/
MIT License
1.88k stars 137 forks source link

Can we use a express middleware in hook? #1012

Closed fahadali503 closed 2 years ago

fahadali503 commented 2 years ago
LoicPoullain commented 2 years ago

It's not possible. Foal is not designed that way. The only thing that you can do is to apply an Express middleware globally to all your application: https://foalts.org/docs/cookbook/expressjs/#pre-and-post-express-middlewares.

fahadali503 commented 2 years ago

Thanks for the response. One more question, can we access userId in express middleware when using @RequireJWT() hook.

LoicPoullain commented 2 years ago

No, it's not possible because all the hooks and the controllers are loaded/executed after the pre-middlewares. In which case would you like to access the userId in one of your middlewares?

fahadali503 commented 2 years ago

I mean when we use @JwtRequired() hook, we get userId in controller. My question is that if i use express middleware, will i get the userId from the request body.

LoicPoullain commented 2 years ago

No, you won't. Because all the application (controllers, hooks and services) are separated from Express. Foal is not designed to be used with Express middlewares.

The only thing that you can do is to put a middleware that is executed before the execution of the application, ie before the execution of the hooks and controllers. This feature only exists for special cases to support Nuxt or Next middlewares for example.

LoicPoullain commented 2 years ago

Looks like this question has been answered. I'm closing the issue 👍