Created
August 10, 2011 21:03
-
-
Save j-mcnally/1138262 to your computer and use it in GitHub Desktop.
Express Middleware for CORS access.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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