From 5983bacc238a6530f2101923e6dd36b8f4f0166b Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Tue, 17 Nov 2015 16:01:17 +0000 Subject: [PATCH] rollback to koa-route@^2 --- example/spa/css/style.css | 2 ++ example/spa/index.html | 4 ++++ lib/local-web-server.js | 17 +++++++++-------- package.json | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/example/spa/css/style.css b/example/spa/css/style.css index 6d3a1e2..793042d 100644 --- a/example/spa/css/style.css +++ b/example/spa/css/style.css @@ -1,3 +1,5 @@ body { background-color: IndianRed; } + +a { color: black } diff --git a/example/spa/index.html b/example/spa/index.html index c2d71a9..9a46a23 100644 --- a/example/spa/index.html +++ b/example/spa/index.html @@ -3,6 +3,10 @@

Single Page App

Location:

+ diff --git a/lib/local-web-server.js b/lib/local-web-server.js index db94de2..0f2aa04 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -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 }) } } diff --git a/package.json b/package.json index 2de092d..751a189 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "koa-etag": "^2.1.0", "koa-morgan": "^0.4.0", "koa-rewrite": "^1.1.1", - "koa-route": "^3", + "koa-route": "^2.4.2", "koa-send": "^3.1.0", "koa-serve-index": "^1.1.0", "koa-static": "^1.5.2",