-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.js
23 lines (20 loc) · 819 Bytes
/
cors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// An excellent explanation of the CORS protocol:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
const express = require('express');
const cors = require('cors'); // see https://www.npmjs.com/package/cors
const app = express();
// add the address of the React or Angular client to the whitelist, example: 'http://localhost:3001'
const whitelist = ['http://localhost:3000', 'https://localhost:3443', 'http://localhost:3001'];
var corsOptionsDelegate = (req, callback) => {
var corsOptions;
console.log(req.header('Origin'));
if(whitelist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true };
}
else {
corsOptions = { origin: false };
}
callback(null, corsOptions);
};
exports.cors = cors();
exports.corsWithOptions = cors(corsOptionsDelegate);