Feature Request
Unanswered
Japanese pilchard posted this in #help-forum
Japanese pilchardOP
I Am missing some features from expressjs in nextjs api. Express allows us to put a function as middleware to a whole routing chain. can we not implement same in Next js , something akin to layout.tsx in app router, something like layout.tsx that i can use in /app/api to run a function belore running the other route.tsx in given tree
9 Replies
File
Also, check its use case once(most people don't view it, but its important to read it)
@Anay-208 | Ping in replies there is middleware.ts
Japanese pilchardOP
Yeah but it's hard to put everything in , I wish we could keep a middleware.ts in every /api folder
@Japanese pilchard I Am missing some features from expressjs in nextjs api. Express allows us to put a function as middleware to a whole routing chain. can we not implement same in Next js , something akin to layout.tsx in app router, something like layout.tsx that i can use in /app/api to run a function belore running the other route.tsx in given tree
What’s your use case for the middleware
Japanese pilchardOP
Let's say for certain routes in api /admin/structure I want to give access to certain admins and restrict some. Surely I can do it in moddleware.ts but imagine a complex usecase where I have 10 of such route groups
Having middlewares everywhere is just matter of convince for me because I am used to express when I can put a function and attach on a route group can decide not to put on something else
Having middlewares everywhere is just matter of convince for me because I am used to express when I can put a function and attach on a route group can decide not to put on something else
@Japanese pilchard Let's say for certain routes in api /admin/structure I want to give access to certain admins and restrict some. Surely I can do it in moddleware.ts but imagine a complex usecase where I have 10 of such route groups
Having middlewares everywhere is just matter of convince for me because I am used to express when I can put a function and attach on a route group can decide not to put on something else
you might have to do that on the page and route level, since middleware isn't really meant for that