Created
November 15, 2022 23:20
-
-
Save miguelmota/e188b88a6e730375bd375b094740ffd4 to your computer and use it in GitHub Desktop.
Node.js express cors whitelist example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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