Reputation: 89
I am running node app with list of whitelisted frontend domains under nginx. Allowed domains are defined in .env file.
app.js:
app.use(cors());
cors.js:
const cors = require('cors');
const whitelist = process.env.CORS_DOMAINS.split(',');
module.exports = (enabled = true) =>
(req, res, next) => {
const options = {
origin(origin, callback) {
if (!origin) {
callback(null, true);
return;
}
const originIsWhitelisted = enabled ? whitelist.indexOf(origin) !== -1 : true;
if (originIsWhitelisted) {
console.log('cors runs');
callback(null, originIsWhitelisted);
return;
}
callback({
statusCode: 401,
error: 'Not allowed',
});
},
};
return cors(options)(req, res, next);
};
How can I update whitelisted domains when app is already running? I have list of domains in the database but don't want to look up every time because of performance.
Upvotes: 3
Views: 1591
Reputation: 5868
You can use express dynamic middleware and use it in runtime with a trigger on database table.
Upvotes: 2