-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (39 loc) · 1.31 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
38
39
40
const httpProxy = require("http-proxy")
const proxy = httpProxy.createProxyServer({
secure: false
})
module.exports = (options) => ({
name: 'debug-proxy',
configureServer(server) {
options = Object.assign(
{
path: '/api',
default: 'https://www.baidu.com',
changeOrigin: true
},
options
)
server.middlewares.use((req, res, next) => {
if (options.path instanceof RegExp === false) {
options.path = new RegExp('^' + options.path)
}
let u = new URL(req.url, 'http://localhost')
if (options.path.test(u.pathname)) {
let urlObj = new URL(req.headers.referer)
var debug = urlObj.search.slice(1).match(new RegExp('(^|&)debug=([^&]*)(&|$)', 'i'))
debug = debug ? debug[2] : options.default
req.url = req.url.replace(options.path, '')
proxy.web(req, res, {
changeOrigin: options.changeOrigin,
target: debug
})
proxy.on('error', (err) => {
res.statusCode = 500
res.end(err.message)
})
} else {
next()
}
})
}
})