Skip to content

Instantly share code, notes, and snippets.

@KolbySisk
Last active December 9, 2022 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KolbySisk/04b069cff98408f95a5bddd707b2aa9a to your computer and use it in GitHub Desktop.
Save KolbySisk/04b069cff98408f95a5bddd707b2aa9a to your computer and use it in GitHub Desktop.
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);
@marxvn
Copy link

marxvn commented Nov 19, 2022

you should also import use function

@KolbySisk
Copy link
Author

Good catch, thanks

@liz-cs
Copy link

liz-cs commented Dec 8, 2022

thank you! btw where to rewrite next()?

@KolbySisk
Copy link
Author

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