origin.js 434 B

12345678910111213141516
  1. exports.origin = (whitelist) => (origin, callback) => {
  2. // Allow requests with no origin (mobile apps, postman, curl)
  3. if (!origin) return callback(null, true)
  4. // Allow exact domain
  5. if (whitelist.includes(origin)) {
  6. return callback(null, true)
  7. }
  8. // Allow localhost with any port
  9. if (origin.startsWith('http://localhost')) {
  10. return callback(null, true)
  11. }
  12. return callback(new Error('Not allowed by CORS'))
  13. }