Skip to content

Instantly share code, notes, and snippets.

@microchipgnu
Created July 2, 2021 11:38
Show Gist options
  • Save microchipgnu/933c9ca3f71bbadc0e5e33d5ab0e4340 to your computer and use it in GitHub Desktop.
Save microchipgnu/933c9ca3f71bbadc0e5e33d5ab0e4340 to your computer and use it in GitHub Desktop.
Allow Cors
const allowCors = (fn) => async (req, res) => {
res.setHeader("Access-Control-Allow-Credentials", true)
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader(
"Access-Control-Allow-Methods",
"GET,OPTIONS,PATCH,DELETE,POST,PUT"
)
res.setHeader(
"Access-Control-Allow-Headers",
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
)
if (req.method === "OPTIONS") {
res.status(200).end()
return
}
return await fn(req, res)
}
export default allowCors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment