Browse Source

upgrade koa-route

master
Lloyd Brookes 9 years ago
parent
commit
f0d36ac89f
  1. 5
      lib/local-web-server.js
  2. 4
      lib/middleware.js
  3. 2
      package.json
  4. 12
      test/test.js

5
lib/local-web-server.js

@ -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 * () {
yield send(this, options.spa, { root: path.resolve(options.static.root) || process.cwd() })
app.use(_.all('*', function spa (ctx, route, next) {
const root = path.resolve(options.static.root) || process.cwd()
return send(ctx, options.spa, { root: root }).then(next)
})) }))
} }
return app return app

4
lib/middleware.js

@ -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 */

2
package.json

@ -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

@ -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({

Loading…
Cancel
Save