forked from gr2m/hapi-cors-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (29 loc) · 1.02 KB
/
index.js
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
30
31
32
33
34
35
36
37
'use strict'
module.exports = addCorsHeaders
function addCorsHeaders (request, h) {
if (!request.headers || !request.headers.origin) {
return h.continue
}
const response = request.response.isBoom &&
request.response.output || request.response
response.headers['access-control-allow-origin'] =
request.headers && request.headers.origin
response.headers['access-control-allow-credentials'] =
'true'
if (request.method !== 'options') {
return h.continue
}
response.statusCode = 200
response.headers['access-control-expose-headers'] =
'content-type, content-length, etag'
response.headers['access-control-max-age'] = 60 * 10
if (request.headers['access-control-request-headers']) {
response.headers['access-control-allow-headers'] =
request.headers['access-control-request-headers']
}
if (request.headers['access-control-request-method']) {
response.headers['access-control-allow-methods'] =
request.headers['access-control-request-method']
}
return h.continue
}