Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
Created August 10, 2011 21:03
Show Gist options
  • Save j-mcnally/1138262 to your computer and use it in GitHub Desktop.
Save j-mcnally/1138262 to your computer and use it in GitHub Desktop.
Express Middleware for CORS access.
module.exports = function preflightApi(){
return function preflightApi(req, res, next){
//this is where the magic happens
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'User-Agent, Accept-Language, Accept-Encoding, Accept-Charset, Connection, Content-Length, Origin, Pragma, Accept-Charset, Cache-Control, Accept, Content-Type, Sessionid');
res.header('Access-Control-Max-Age', '1000');
if (req.method == 'OPTIONS') {
res.header('Content-Type', 'text/plain');
res.writeHead(200);
res.end();
}
else {
next();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment