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