Answers for "nextjs Access-Control-Allow-Origin"

0

nextjs Access-Control-Allow-Origin

//npm i cors
//# or
//yarn add cors


import Cors from 'cors'

// Initializing the cors middleware
const cors = Cors({
  methods: ['GET', 'HEAD'],
})

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
  return new Promise((resolve, reject) => {
    fn(req, res, (result) => {
      if (result instanceof Error) {
        return reject(result)
      }

      return resolve(result)
    })
  })
}

async function handler(req, res) {
  // Run the middleware
  await runMiddleware(req, res, cors)

  // Rest of the API logic
  res.json({ message: 'Hello Everyone!' })
}

export default handler
Posted by: Guest on August-10-2021

Code answers related to "nextjs Access-Control-Allow-Origin"

Code answers related to "Javascript"

Browse Popular Code Answers by Language