proxy connection errors no longer crash the server, fixes #37
This commit is contained in:
@ -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}`)
|
||||
|
||||
proxy.once('error', err => {
|
||||
this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
|
||||
return new Promise((resolve, reject) => {
|
||||
proxy.once('error', err => {
|
||||
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 })
|
||||
})
|
||||
proxy.once('proxyReq', function (proxyReq) {
|
||||
proxyReq.path = url.parse(route.new).path
|
||||
})
|
||||
|
||||
proxy.web(this.req, this.res, { target: route.new })
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user