Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created June 29, 2019 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BetterProgramming/d2610251b929bb2093a90f8c45f0221f to your computer and use it in GitHub Desktop.
Save BetterProgramming/d2610251b929bb2093a90f8c45f0221f to your computer and use it in GitHub Desktop.
var express = require('express')
,cors = require('cors')
, app = express();
//app.user(bodyParser.json());
// after the code that uses bodyParser and other cool stuff
var originsWhitelist = [
'http://localhost:4200', //this is my front-end url for development
'http://www.myproductionurl.com'
];
var corsOptions = {
origin: function(origin, callback){
var isWhitelisted = originsWhitelist.indexOf(origin) !== -1;
callback(null, isWhitelisted);
},
credentials:true
}
//here is the magic
app.use(cors(corsOptions));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment