-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·62 lines (55 loc) · 1.51 KB
/
Copy pathindex.js
File metadata and controls
executable file
·62 lines (55 loc) · 1.51 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env node
const express = require('express')
const { createProxyMiddleware } = require('http-proxy-middleware')
const cors = require('cors')
const DEV = process.env.NODE_ENV && process.env.NODE_ENV !== 'production'
if (DEV || process.env.DOTENV) {
require('dotenv').config()
}
const {
PORT = '5555',
TARGET,
CORS_ORIGIN = 'true',
COOKIE_DOMAIN_REWRITE = '*:localhost',
COOKIE_STRIP_SECURE = 'true'
} = process.env
const app = express()
const parseEnv = (string, parser = identity => identity) => {
if (string === 'true' || string === '1') {
return true
}
if (string === 'false' || string === '0') {
return false
}
return parser(string)
}
app.use(cors({
origin: parseEnv(CORS_ORIGIN, string => string.split(',')),
credentials: true
}))
app.use(
'/',
createProxyMiddleware({
target: TARGET,
changeOrigin: true,
cookieDomainRewrite:
parseEnv(COOKIE_DOMAIN_REWRITE, string =>
string.split(',').reduce((rules, rewrite) => {
const [source, target] = rewrite.split(':')
rules[source] = target
return rules
}, {})
),
onProxyRes: (proxyRes) => {
if (parseEnv(COOKIE_STRIP_SECURE) === true) {
proxyRes.headers['set-cookie'] = (proxyRes.headers['set-cookie'] || [])
.map(header => header.replace('; Secure', ''))
.map(header => header.replace('; SameSite=None', '; SameSite=Lax'))
}
}
})
)
app.listen(PORT, (err) => {
if (err) throw err
console.log(`> Ready on port ${PORT}`)
})