Skip to content

Commit

Permalink
fix: Fix CORS handling
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose committed Sep 20, 2018
1 parent 4fa1141 commit 9b84000
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const cors = require('./micro-cors.js')({
allowHeaders,
exposeHeaders,
allowMethods,
allowCredentials: false,
origin
})
const allow = require('./allow-request.js')
Expand Down Expand Up @@ -86,6 +87,8 @@ async function service (req, res) {
// Don't waste my precious bandwidth
return send(res, 403, '')
}

// Handle CORS preflight request
if (req.method === 'OPTIONS') {
return send(res, 200, '')
}
Expand Down Expand Up @@ -120,4 +123,4 @@ async function service (req, res) {
f.body.pipe(res)
}

module.exports = cors(service)
module.exports = cors(service)
6 changes: 5 additions & 1 deletion micro-cors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// MIT License
// https://github.com/possibilities/micro-cors
// source: https://github.com/possibilities/micro-cors/pull/42
const DEFAULT_ALLOW_METHODS = [
'POST',
'GET',
Expand All @@ -26,11 +27,14 @@ const cors = (options = {}) => handler => (req, res, ...restArgs) => {
maxAge = DEFAULT_MAX_AGE_SECONDS,
allowMethods = DEFAULT_ALLOW_METHODS,
allowHeaders = DEFAULT_ALLOW_HEADERS,
allowCredentials = true,
exposeHeaders = []
} = options

res.setHeader('Access-Control-Allow-Origin', origin)
res.setHeader('Access-Control-Allow-Credentials', 'true')
if (allowCredentials) {
res.setHeader('Access-Control-Allow-Credentials', 'true')
}
if (exposeHeaders.length) {
res.setHeader('Access-Control-Expose-Headers', exposeHeaders.join(','))
}
Expand Down

0 comments on commit 9b84000

Please sign in to comment.