Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save farvisun/75fd1db16c70cee1ba12ee66cce63e83 to your computer and use it in GitHub Desktop.

Select an option

Save farvisun/75fd1db16c70cee1ba12ee66cce63e83 to your computer and use it in GitHub Desktop.

Revisions

  1. Jason revised this gist Apr 9, 2012. No changes.
  2. Jason created this gist Apr 9, 2012.
    19 changes: 19 additions & 0 deletions node-express-cors-middleware.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    app.use(express.methodOverride());

    // ## CORS middleware
    //
    // see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
    var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
    res.send(200);
    }
    else {
    next();
    }
    };
    app.use(allowCrossDomain);