From 99305cb78abd5bdacfe83a69cbbb51ec500a45d7 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sun, 15 Nov 2015 14:53:25 +0000 Subject: [PATCH] SPA option added --- bin/cli.js | 11 ++++++++--- lib/local-web-server.js | 8 ++++++++ package.json | 1 + test/fixture/spa/.local-web-server.json | 3 +++ test/fixture/spa/css/style.css | 3 +++ test/fixture/spa/one.html | 1 + test/fixture/spa/spa.html | 8 ++++++++ 7 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/fixture/spa/.local-web-server.json create mode 100644 test/fixture/spa/css/style.css create mode 100644 test/fixture/spa/one.html create mode 100644 test/fixture/spa/spa.html diff --git a/bin/cli.js b/bin/cli.js index dc5b410..11f2a07 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,5 +1,7 @@ #!/usr/bin/env node 'use strict' +const s = Date.now() + const localWebServer = require('../') const commandLineArgs = require('command-line-args') const ansi = require('ansi-escape-sequences') @@ -43,7 +45,8 @@ localWebServer({ compress: options.cli.server.compress, mime: options.cli.server.mime, blacklist: options.cli.server.blacklist.map(regexp => RegExp(regexp, "i")), - proxyRoutes: options.cli.server.proxyRoutes + proxyRoutes: options.cli.server.proxyRoutes, + spa: options.cli.server.spa }).listen(options.cli.server.port, onServerUp) function halt (message) { @@ -53,9 +56,11 @@ function halt (message) { } function onServerUp () { + const e = Date.now() + const time = `${e-s}ms` console.error(ansi.format( path.resolve(options.cli.server.directory) === process.cwd() - ? `serving at [underline]{http://localhost:${options.cli.server.port}}` - : `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}` + ? `serving at [underline]{http://localhost:${options.cli.server.port}} ${time}` + : `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}} ${time}` )) } diff --git a/lib/local-web-server.js b/lib/local-web-server.js index a5a3e1b..df4dbf2 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -16,6 +16,7 @@ const _ = require('koa-route') const mount = require('koa-mount') const httpProxy = require('http-proxy') const pathToRegexp = require('path-to-regexp') +const send = require('koa-send') /** * @module local-web-server @@ -125,6 +126,7 @@ function getApp (options) { /* serve static files */ if (options.static.root) { app.use(serve(options.static.root, options.static.options)) + // options.static.root.forEach(config => { // app.use(mount(config.route, serve(config.root))) // app.use(mount(config.route, serveIndex(config.root))) @@ -136,6 +138,12 @@ function getApp (options) { app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) } + /* for any URL not matched by static (e.g. `/search`), serve the SPA */ + if (options.spa) { + app.use(_.all('*', function * () { + yield send(this, options.spa, { root: process.cwd() }) + })) + } return app } diff --git a/package.json b/package.json index b42e292..e394031 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "koa-mount": "^1.3.0", "koa-rewrite": "^1.1.1", "koa-route": "^2.4.2", + "koa-send": "^3.1.0", "koa-serve-index": "^1.1.0", "koa-static": "^1.5.2", "morgan": "^1.0.0", diff --git a/test/fixture/spa/.local-web-server.json b/test/fixture/spa/.local-web-server.json new file mode 100644 index 0000000..881eafa --- /dev/null +++ b/test/fixture/spa/.local-web-server.json @@ -0,0 +1,3 @@ +{ + "spa": "spa.html" +} diff --git a/test/fixture/spa/css/style.css b/test/fixture/spa/css/style.css new file mode 100644 index 0000000..6d3a1e2 --- /dev/null +++ b/test/fixture/spa/css/style.css @@ -0,0 +1,3 @@ +body { + background-color: IndianRed; +} diff --git a/test/fixture/spa/one.html b/test/fixture/spa/one.html new file mode 100644 index 0000000..0f04799 --- /dev/null +++ b/test/fixture/spa/one.html @@ -0,0 +1 @@ +

one

diff --git a/test/fixture/spa/spa.html b/test/fixture/spa/spa.html new file mode 100644 index 0000000..94a709a --- /dev/null +++ b/test/fixture/spa/spa.html @@ -0,0 +1,8 @@ + + + +

Single Page App

+

Location:

+