Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created April 5, 2013 12:03
Show Gist options
  • Select an option

  • Save simenbrekken/5318792 to your computer and use it in GitHub Desktop.

Select an option

Save simenbrekken/5318792 to your computer and use it in GitHub Desktop.
CORS Header Test
var http = require('http')
var challenge = 'Basic ' + (new Buffer('test:test').toString('base64'))
http.createServer(function(req, res) {
var headers = {
'Access-Control-Allow-Origin': req.headers.origin,
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'authentication',
'Access-Control-Max-Age': 60 * 25 * 30,
'Content-Type': 'application/json'
}
if (req.method === 'OPTIONS') {
res.writeHead(200, headers)
res.end()
} else {
var authenticated = req.headers.authentication == challenge
res.writeHead(authenticated ? 200 : 401, headers)
res.end(JSON.stringify({
headers: req.headers,
authenticated: authenticated
}))
}
}).listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment