hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.62k stars 65 forks source link

How to use different middlewares for each method on the same handler #183

Closed RadoslavMarinov closed 2 years ago

RadoslavMarinov commented 2 years ago

In Express we can do it by adding the middleware into the method like so:

app.get('/user/:userId', getMiddleware, (req,res)=>.... app.delete('/user/:userId', deleteMiddleware, (req,res)=>....

How to do the trick in next-connect? If I apply middleware to the GET it will always trigger for the DELETE method as well. I want really separate independent middleware for the two methods

RadoslavMarinov commented 2 years ago

My mistake it is possible actually. This works

handler.get(...validateGetReq(), async(req:NextApiRequest, res:NextApiResponse)=>{ handler.delete(...validateDeleteReq(),async(req:NextApiRequest, res:NextApiResponse)=>{