Skip to content

Instantly share code, notes, and snippets.

@fnakstad
Last active December 18, 2015 09:18
Show Gist options
  • Save fnakstad/5759949 to your computer and use it in GitHub Desktop.
Save fnakstad/5759949 to your computer and use it in GitHub Desktop.
module.exports.allowCrossDomain = function(req, res, next) {
var oneof = false
, clientUrl = 'http://myclient.com';
if (req.headers.origin) {
res.header('Access-Control-Allow-Origin', clientUrl);
res.header('Access-Control-Allow-Credentials', true);
oneof = true;
}
if (req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', 'GET, POST');
oneof = true;
}
if (req.headers['access-control-request-headers']) {
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
oneof = true;
}
if (oneof) {
res.header('Access-Control-Max-Age', 60 * 60 * 24 * 365);
}
if (oneof && req.method == 'OPTIONS') res.send(200);
else next();
};
// ...
app.use(require('./config/crossdomain').allowCrossDomain);
// ... Set up routing, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment