unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
6.12k stars 503 forks source link

[FEATURE] Server middlewares should have fine-grained control like client middlewares #1735

Closed ghoshRitesh12 closed 1 year ago

ghoshRitesh12 commented 1 year ago

Describe the feature

In Nuxt, I have 2 middlewares in /middlewares dir, namely native and admin and I can use them using the definePageMeta() macro in the pages that I prefer. I have a refined control over the client middlewares and which pages I want them to be in.

image

It would be great to have this refined control for server middlewares as well. In /server/middlewares, I should be able to define middlewares and use them for specific server routes and NOT for all server routes, which is the default behavior. I should also be able to chain middlewares, much like in the client side.

Additional information

pi0 commented 1 year ago

Hi. Thanks for opening issue and explaining but Nuxt middleware are Powered by Vue router and completely different than Nitro/Server middleware.

Generally, I would recommend you prefer server/utils to make composables used in each handler instead of global server middleware.

You can also have an if condition in server middleware to filter them out based on event.path or event.method (which I still won't recommend again unless necessary).

Also, you can use new h3 handler object syntax to set multiple middleware per route (https://unjs.io/blog/2023-08-15-h3-towards-the-edge-of-the-web#object-syntax-event-handlers)