Last active
December 9, 2022 03:21
-
-
Save KolbySisk/04b069cff98408f95a5bddd707b2aa9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { use, Middleware } from 'next-api-route-middleware'; | |
export const allowMethods = (allowedMethods: string[]): Middleware => { | |
return async function (req, res, next) { | |
if (allowedMethods.includes(req.method!) || req.method == 'OPTIONS') { | |
next(); | |
} else { | |
res.status(405).send({ message: 'Method not allowed.' }); | |
} | |
}; | |
}; | |
export default use(allowMethods['GET'], handler); |
next
is made available as a parameter to the middleware function, along with req
and res
. You call it when your middleware has finished and it will invoke the next middleware function, or the handler if there are no more middleware functions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you! btw where to rewrite next()?