Skip to content

Commit

Permalink
Move CORS headers into their own middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBlueHat committed May 11, 2017
1 parent 7cabb76 commit 0e961e5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 0 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,6 @@ console.log(config);

app.use(function(req, res, next) {
res.header('X-Powered-By', mayktsoURI);
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
if(req.header('Origin')) {
res.header("Access-Control-Allow-Origin", req.header('Origin'));
}
else {
res.header("Access-Control-Allow-Origin", "*");
}
res.header("Access-Control-Allow-Headers", "Content-Length, Content-Type, If-None-Match, Link, Location, Origin, Slug, X-Requested-With");
res.header("Access-Control-Expose-Headers", "Accept-Post, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Allow, Content-Length, Content-Type, ETag, Last-Modified, Link, Location, Updates-Via, Vary");
return next();
});

Expand Down
13 changes: 13 additions & 0 deletions src/server/cors-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = function(req, res, next) {
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
if(req.header('Origin')) {
res.header("Access-Control-Allow-Origin", req.header('Origin'));
}
else {
res.header("Access-Control-Allow-Origin", "*");
}
res.header("Access-Control-Allow-Headers", "Content-Length, Content-Type, If-None-Match, Link, Location, Origin, Slug, X-Requested-With");
res.header("Access-Control-Expose-Headers", "Accept-Post, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Allow, Content-Length, Content-Type, ETag, Last-Modified, Link, Location, Updates-Via, Vary");
return next();
};
2 changes: 2 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exports.createServer = function(config){
var app = express();
// app.use(compress());

app.use(require('./cors-headers.js'));

if (config.sslKey && config.sslCert) {
var options = {
key: fs.readFileSync(config.sslKey),
Expand Down

0 comments on commit 0e961e5

Please sign in to comment.