showing results for - "nextjs access control allow origin"
Victoria
26 Jul 2017
1//npm i cors
2//# or
3//yarn add cors
4
5
6import Cors from 'cors'
7
8// Initializing the cors middleware
9const cors = Cors({
10  methods: ['GET', 'HEAD'],
11})
12
13// Helper method to wait for a middleware to execute before continuing
14// And to throw an error when an error happens in a middleware
15function runMiddleware(req, res, fn) {
16  return new Promise((resolve, reject) => {
17    fn(req, res, (result) => {
18      if (result instanceof Error) {
19        return reject(result)
20      }
21
22      return resolve(result)
23    })
24  })
25}
26
27async function handler(req, res) {
28  // Run the middleware
29  await runMiddleware(req, res, cors)
30
31  // Rest of the API logic
32  res.json({ message: 'Hello Everyone!' })
33}
34
35export default handler
36