Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created November 15, 2022 23:20
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 miguelmota/e188b88a6e730375bd375b094740ffd4 to your computer and use it in GitHub Desktop.
Save miguelmota/e188b88a6e730375bd375b094740ffd4 to your computer and use it in GitHub Desktop.
Node.js express cors whitelist example
const whitelist = ['http://localhost:3000']
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.includes(origin)) {
callback(null, true)
} else {
console.log('origin:', origin, 'not allowed')
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors(corsOptions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment