Created
August 18, 2013 19:37
-
-
Save pbojinov/6263558 to your computer and use it in GitHub Desktop.
CORS middleware for Node.js Express
This file contains hidden or 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
var allowCrossDomain = function(req, res, next) { | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization, Accept'); | |
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS,TRACE'); | |
res.header('Access-Control-Allow-Credentials', true); | |
/** | |
* CORS OPTIONS request, simply return 200 | |
* --- | |
* Browser will try to see request is possible before actually doing it | |
* We respond back with 200 so we don't break basic auth | |
*/ | |
if (req.method == 'OPTIONS') { | |
res.statusCode = 200; | |
res.end(); | |
return; | |
} | |
next(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment