Skip to content

Instantly share code, notes, and snippets.

@marxian
Created October 27, 2020 13:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marxian/f7ccbecde5e3cb986845030860c84062 to your computer and use it in GitHub Desktop.
Save marxian/f7ccbecde5e3cb986845030860c84062 to your computer and use it in GitHub Desktop.
import Cors from 'cors'
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
const cors = Cors({
methods: ['POST'],
})
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, result => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
await runMiddleware(req, res, cors)
res.status(200).json(['some', 'stuff'])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment