|
@@ -5,6 +5,7 @@ const cookiejar = require('cookiejar')
|
|
|
const host = process.env.HOST || '127.0.0.1'
|
|
const host = process.env.HOST || '127.0.0.1'
|
|
|
const port = process.env.PORT || 3000
|
|
const port = process.env.PORT || 3000
|
|
|
process.noDeprecation = true
|
|
process.noDeprecation = true
|
|
|
|
|
+const url = require('url')
|
|
|
|
|
|
|
|
app.set('port', port)
|
|
app.set('port', port)
|
|
|
|
|
|
|
@@ -16,39 +17,41 @@ config.dev = !(process.env.NODE_ENV === 'production')
|
|
|
const proxyTable = config.proxyTable
|
|
const proxyTable = config.proxyTable
|
|
|
if (proxyTable) {
|
|
if (proxyTable) {
|
|
|
// 本地代理支持localhost、127.0.0.1等不同地址跨域
|
|
// 本地代理支持localhost、127.0.0.1等不同地址跨域
|
|
|
- app.use((req, res, next, store) => {
|
|
|
|
|
|
|
+ app.use((req, res, next) => {
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
|
|
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
|
|
|
res.header('Access-Control-Allow-Headers', 'Content-Type')
|
|
res.header('Access-Control-Allow-Headers', 'Content-Type')
|
|
|
res.header('Access-Control-Allow-Credentials', 'true')
|
|
res.header('Access-Control-Allow-Credentials', 'true')
|
|
|
- if (res.req.headers.referer && res.req.headers.referer.substring(res.req.headers.referer.length - 9, res.req.headers.referer.length) === '?type=erp') {
|
|
|
|
|
|
|
+ const refer = res.req.headers.referer
|
|
|
|
|
+ if (refer && url.parse(refer, true).query.type === 'erp') {
|
|
|
res.cookie('type', 'erp')
|
|
res.cookie('type', 'erp')
|
|
|
}
|
|
}
|
|
|
next()
|
|
next()
|
|
|
})
|
|
})
|
|
|
|
|
+ const defaultOptions = {
|
|
|
|
|
+ target: config.env.baseUrl,
|
|
|
|
|
+ changeOrigin: true,
|
|
|
|
|
+ onProxyRes: (proxyRes) => {
|
|
|
|
|
+ const setCookieHeaders = proxyRes.headers['set-cookie'] || []
|
|
|
|
|
+ const modifiedSetCookieHeaders = setCookieHeaders
|
|
|
|
|
+ .map(str => new cookiejar.Cookie(str))
|
|
|
|
|
+ .map(cookie => {
|
|
|
|
|
+ cookie.path = '/'
|
|
|
|
|
+ return cookie
|
|
|
|
|
+ })
|
|
|
|
|
+ .map(cookie => cookie.toString())
|
|
|
|
|
+ proxyRes.headers['set-cookie'] = modifiedSetCookieHeaders
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
if (Array.isArray(proxyTable)) {
|
|
if (Array.isArray(proxyTable)) {
|
|
|
- app.use(proxy(proxyTable, {
|
|
|
|
|
- target: config.env.baseUrl,
|
|
|
|
|
- changeOrigin: true,
|
|
|
|
|
- onProxyRes: (proxyRes) => {
|
|
|
|
|
- const setCookieHeaders = proxyRes.headers['set-cookie'] || []
|
|
|
|
|
- const modifiedSetCookieHeaders = setCookieHeaders
|
|
|
|
|
- .map(str => new cookiejar.Cookie(str))
|
|
|
|
|
- .map(cookie => {
|
|
|
|
|
- cookie.path = '/'
|
|
|
|
|
- return cookie
|
|
|
|
|
- })
|
|
|
|
|
- .map(cookie => cookie.toString())
|
|
|
|
|
- proxyRes.headers['set-cookie'] = modifiedSetCookieHeaders
|
|
|
|
|
- }
|
|
|
|
|
- }))
|
|
|
|
|
|
|
+ app.use(proxy(proxyTable, defaultOptions))
|
|
|
} else {
|
|
} else {
|
|
|
Object.keys(proxyTable).forEach((context) => {
|
|
Object.keys(proxyTable).forEach((context) => {
|
|
|
var options = proxyTable[context]
|
|
var options = proxyTable[context]
|
|
|
if (typeof options === 'string') {
|
|
if (typeof options === 'string') {
|
|
|
- options = { target: options }
|
|
|
|
|
|
|
+ options = {target: options}
|
|
|
}
|
|
}
|
|
|
- app.use(proxy(context, options))
|
|
|
|
|
|
|
+ app.use(proxy(context, Object.assign(defaultOptions, options)))
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
// axios use proxy url
|
|
// axios use proxy url
|