Skip to content

Instantly share code, notes, and snippets.

@bobbysciacchitano
Created April 26, 2015 10:32
Show Gist options
  • Save bobbysciacchitano/8af6872e14dd37ab802f to your computer and use it in GitHub Desktop.
Save bobbysciacchitano/8af6872e14dd37ab802f to your computer and use it in GitHub Desktop.
Express CORS Middleware
var middleware = function(request, response, next) {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
response.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
response.header("Access-Control-Max-Age", "432000");
if(request.method === 'OPTIONS') {
response.status(200).send();
return;
}
next();
};
module.exports = middleware;
@bobbysciacchitano
Copy link
Author

You can adjust this to your taste for instance if you only want to expose methods for a specific resource, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment