Skip to content

Instantly share code, notes, and snippets.

@yoannmoinet
Last active August 29, 2015 14:05
Show Gist options
  • Save yoannmoinet/74912275dca5e83081b4 to your computer and use it in GitHub Desktop.
Save yoannmoinet/74912275dca5e83081b4 to your computer and use it in GitHub Desktop.
Add all requested headers for a CORS request.
//Cross Domain Headers.
//A bit hacky on the referer.
var getCorsHeaders = function (req, res){
//Can't put wildcards here,
//so here we are, cheating all the way.
var ref = req.headers.referer || req.headers.origin;
ref = ref?ref.slice(0,-1):"*";
res.set('Access-Control-Allow-Origin', ref);
res.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.set('Access-Control-Allow-Headers', 'Authorization, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, origin');
res.set('Access-Control-Allow-Credentials', true);
res.set('Content-Type', 'application/json');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment