Skip to content

Instantly share code, notes, and snippets.

@botic
Last active December 11, 2015 17:19
Show Gist options
  • Select an option

  • Save botic/4633909 to your computer and use it in GitHub Desktop.

Select an option

Save botic/4633909 to your computer and use it in GitHub Desktop.
Enable CORS in Stick applications. For details see: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
var config = require("./config/config");
exports.middleware = function(next, app) {
return function accessControl(request) {
var response;
if (request.method === "OPTIONS") {
// Handle possible preflight requests
response = {
status: 204, // NO CONTENT
headers: {
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, Content-Type",
"Access-Control-Max-Age": 120
},
body: [""]
};
} else {
response = next(request);
}
// Globally set the Allow-Origin header for CORS
response.headers["Access-Control-Allow-Origin"] = (config.get("security").enableCORS || "*");
return response;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment