-
Notifications
You must be signed in to change notification settings - Fork 30
/
proxy.js
33 lines (29 loc) · 880 Bytes
/
proxy.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
'use strict'
var httpProxy = require('http-proxy')
var _this = this
function Proxy (target, paths) {
if (!(this instanceof Proxy)) return new Proxy(target, paths)
_this.proxyTarget = target
_this.proxyPaths = paths
_this.proxy = httpProxy.createProxyServer({ changeOrigin: true })
}
Proxy.prototype.middleFunc = function (req, res, next) {
var matchProxy = false
for (var i = 0, len = _this.proxyPaths.length; i < len; i++) {
if (req.url.startsWith(_this.proxyPaths[i])) {
matchProxy = true
break
}
}
if (!matchProxy) {
return next()
}
_this.proxy.web(req, res, { target: _this.proxyTarget }, function (err, req, res) {
console.error('failed to connect to proxy: ' + _this.proxyTarget + ' - ' + err.message)
res.writeHead(500, {
'Content-Type': 'text/plain'
})
res.end(err.stack)
})
}
module.exports = Proxy