All files / take-five/lib cors.js

95.24% Statements 20/21
90% Branches 9/10
100% Functions 2/2
95.24% Lines 20/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301x 1x 1x 1x   1x 5x 5x 1x   5x 1x   5x       5x 20x 20x 20x 20x 20x 1x 1x   19x      
const headers = ['Content-Type', 'Accept', 'X-Requested-With']
let origin = '*'
let credentials = true
const methods = ['PUT', 'POST', 'DELETE', 'GET', 'OPTIONS']
 
module.exports = function (opts) {
  origin = opts.origin || origin
  if (opts.credentials === false) {
    credentials = false
  }
  if (Array.isArray(opts.headers)) {
    headers.push.apply(headers, opts.headers)
  }
  Iif (Array.isArray(opts.methods)) {
    methods.push.appy(methods, opts.methods)
  }
 
  return function cors (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', origin)
    res.setHeader('Access-Control-Allow-Headers', headers.join(','))
    res.setHeader('Access-Control-Allow-Credentials', credentials)
    res.setHeader('Access-Control-Allow-Methods', methods.join(','))
    if (req.method === 'OPTIONS') {
      res.statusCode = 204
      return res.end()
    }
    next()
  }
}