rollback to koa-route@^2

This commit is contained in:
Lloyd Brookes
2015-11-17 16:01:17 +00:00
parent 694061b28f
commit 5983bacc23
4 changed files with 16 additions and 9 deletions

View File

@ -34,6 +34,7 @@ function localWebServer (options) {
options = Object.assign({
static: {},
serveIndex: {},
spa: null,
log: {},
compress: false,
mime: {},
@ -129,7 +130,7 @@ function localWebServer (options) {
if (options.spa) {
const send = require('koa-send')
app.use(_.all('*', function * () {
yield send(this, options.spa, { root: process.cwd() })
yield send(this, options.spa, { root: options.static.root || process.cwd() })
}))
}
return app
@ -146,35 +147,35 @@ function proxyRequest (route) {
changeOrigin: true
})
return function proxyMiddleware (ctx) {
return function * proxyMiddleware () {
const next = arguments[arguments.length - 1]
const keys = []
route.re = pathToRegexp(route.from, keys)
route.new = ctx.path.replace(route.re, route.to)
route.new = this.path.replace(route.re, route.to)
keys.forEach((key, index) => {
const re = RegExp(`:${key.name}`, 'g')
route.new = route.new
.replace(re, arguments[index + 1] || '')
.replace(re, arguments[index] || '')
})
/* test no keys remain in the new path */
keys.length = 0
pathToRegexp(route.new, keys)
if (keys.length) {
ctx.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
return next()
}
ctx.response = false
this.response = false
proxy.once('error', err => {
ctx.throw(500, `[PROXY] ${err.message}: ${route.new}`)
this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
})
proxy.once('proxyReq', function (proxyReq) {
proxyReq.path = url.parse(route.new).path;
})
proxy.web(ctx.req, ctx.res, { target: route.new })
proxy.web(this.req, this.res, { target: route.new })
}
}