upgrade koa-route
This commit is contained in:
@ -162,8 +162,9 @@ function localWebServer (options) {
|
|||||||
if (options.spa) {
|
if (options.spa) {
|
||||||
const send = require('koa-send')
|
const send = require('koa-send')
|
||||||
debug('SPA', options.spa)
|
debug('SPA', options.spa)
|
||||||
app.use(_.all('*', function * () {
|
app.use(_.all('*', function spa (ctx, route, next) {
|
||||||
yield send(this, options.spa, { root: path.resolve(options.static.root) || process.cwd() })
|
const root = path.resolve(options.static.root) || process.cwd()
|
||||||
|
return send(ctx, options.spa, { root: root }).then(next)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
return app
|
return app
|
||||||
|
@ -20,7 +20,7 @@ function proxyRequest (route, app) {
|
|||||||
changeOrigin: true
|
changeOrigin: true
|
||||||
})
|
})
|
||||||
|
|
||||||
return function * proxyMiddleware () {
|
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)
|
||||||
@ -29,7 +29,7 @@ function proxyRequest (route, app) {
|
|||||||
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] || '')
|
.replace(re, arguments[index + 1] || '')
|
||||||
})
|
})
|
||||||
|
|
||||||
/* test no keys remain in the new path */
|
/* test no keys remain in the new path */
|
||||||
|
@ -45,7 +45,7 @@
|
|||||||
"koa-json": "75lb/json",
|
"koa-json": "75lb/json",
|
||||||
"koa-morgan": "^0.4.0",
|
"koa-morgan": "^0.4.0",
|
||||||
"koa-rewrite": "75lb/rewrite",
|
"koa-rewrite": "75lb/rewrite",
|
||||||
"koa-route": "^2.4.2",
|
"koa-route": "^3",
|
||||||
"koa-send": "^3.1.0",
|
"koa-send": "^3.1.0",
|
||||||
"koa-serve-index": "^1.1.0",
|
"koa-serve-index": "^1.1.0",
|
||||||
"koa-static": "^1.5.2",
|
"koa-static": "^1.5.2",
|
||||||
|
12
test/test.js
12
test/test.js
@ -165,6 +165,18 @@ test('rewrite: proxy', function (t) {
|
|||||||
}})
|
}})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('rewrite: proxy, two url tokens', function (t) {
|
||||||
|
t.plan(2)
|
||||||
|
const app = localWebServer({
|
||||||
|
log: { format: 'none' },
|
||||||
|
rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ]
|
||||||
|
})
|
||||||
|
launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => {
|
||||||
|
t.strictEqual(response.res.statusCode, 200)
|
||||||
|
t.ok(/command-line-args/.test(response.data))
|
||||||
|
}})
|
||||||
|
})
|
||||||
|
|
||||||
test('rewrite: proxy with port', function (t) {
|
test('rewrite: proxy with port', function (t) {
|
||||||
t.plan(2)
|
t.plan(2)
|
||||||
const one = localWebServer({
|
const one = localWebServer({
|
||||||
|
Reference in New Issue
Block a user