Skip to content

Instantly share code, notes, and snippets.

@jaawerth
Last active August 29, 2015 14:13
Show Gist options
  • Save jaawerth/69ca99d67f531af1971e to your computer and use it in GitHub Desktop.
Save jaawerth/69ca99d67f531af1971e to your computer and use it in GitHub Desktop.
Super simple middleware for allowing CORS from express
app.use(allowCrossDomain); // Use below function as a middleware for your requests
function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment