Skip to content

Instantly share code, notes, and snippets.

@akhawaja
Created November 15, 2015 22:58
Show Gist options
  • Save akhawaja/d301728a2eb98e27b486 to your computer and use it in GitHub Desktop.
Save akhawaja/d301728a2eb98e27b486 to your computer and use it in GitHub Desktop.
Express.js CORS OPTIONS Endpoint
var express = require('express'),
app = express();
// More variables initialized...
// More routes, settings, etc.
// This is the OPTIONS endpoint
app.options('*', function (req, res, next) {
origin = req.headers.origin;
if (origin == undefined) {
origin = '*';
}
res.header({'Cache-Control': 'no-store'});
res.header({'Pragma': 'no-cache'});
res.header({'Access-Control-Allow-Origin': origin});
res.header({'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, HEAD, OPTIONS'});
res.header({'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization'});
if (origin != '*') {
res.header({'Access-Control-Allow-Credentials': true});
} else {
res.header({'Access-Control-Allow-Credentials': false});
}
res.status(200);
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment