Created
November 15, 2015 22:58
-
-
Save akhawaja/d301728a2eb98e27b486 to your computer and use it in GitHub Desktop.
Express.js CORS OPTIONS Endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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