|
|
@ -15,14 +15,13 @@ exports.blacklist = blacklist |
|
|
|
exports.mockResponses = mockResponses |
|
|
|
exports.mime = mime |
|
|
|
|
|
|
|
function proxyRequest (route, app) { |
|
|
|
function proxyRequest (route) { |
|
|
|
const httpProxy = require('http-proxy') |
|
|
|
const proxy = httpProxy.createProxyServer({ |
|
|
|
changeOrigin: true |
|
|
|
}) |
|
|
|
|
|
|
|
return function proxyMiddleware () { |
|
|
|
const next = arguments[arguments.length - 1] |
|
|
|
const keys = [] |
|
|
|
route.re = pathToRegexp(route.from, keys) |
|
|
|
route.new = this.url.replace(route.re, route.to) |
|
|
@ -33,17 +32,19 @@ function proxyRequest (route, app) { |
|
|
|
.replace(re, arguments[index + 1] || '') |
|
|
|
}) |
|
|
|
|
|
|
|
this.response = false |
|
|
|
debug('proxy request', `from: ${this.path}, to: ${url.parse(route.new).href}`) |
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
proxy.once('error', err => { |
|
|
|
this.throw(500, `[PROXY] ${err.message}: ${route.new}`) |
|
|
|
err.message = `[PROXY] Error: ${err.message} Target: ${route.new}` |
|
|
|
reject(err) |
|
|
|
}) |
|
|
|
proxy.once('proxyReq', function (proxyReq) { |
|
|
|
proxyReq.path = url.parse(route.new).path |
|
|
|
}) |
|
|
|
|
|
|
|
proxy.once('close', resolve) |
|
|
|
proxy.web(this.req, this.res, { target: route.new }) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|