Created
June 8, 2013 12:30
-
-
Save slav123/5735012 to your computer and use it in GitHub Desktop.
node.js express - allow CORS
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
app.use(function(req, res, next) { | |
var oneof = false; | |
if(req.headers.origin) { | |
res.header('Access-Control-Allow-Origin', req.headers.origin); | |
oneof = true; | |
} | |
if(req.headers['access-control-request-method']) { | |
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']); | |
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); | |
} | |
// intercept OPTIONS method | |
if (oneof && req.method == 'OPTIONS') { | |
res.send(200); | |
} | |
else { | |
next(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment