Browse Source

SPA option added

master
Lloyd Brookes 9 years ago
parent
commit
99305cb78a
  1. 11
      bin/cli.js
  2. 8
      lib/local-web-server.js
  3. 1
      package.json
  4. 3
      test/fixture/spa/.local-web-server.json
  5. 3
      test/fixture/spa/css/style.css
  6. 1
      test/fixture/spa/one.html
  7. 8
      test/fixture/spa/spa.html

11
bin/cli.js

@ -1,5 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict' 'use strict'
const s = Date.now()
const localWebServer = require('../') const localWebServer = require('../')
const commandLineArgs = require('command-line-args') const commandLineArgs = require('command-line-args')
const ansi = require('ansi-escape-sequences') const ansi = require('ansi-escape-sequences')
@ -43,7 +45,8 @@ localWebServer({
compress: options.cli.server.compress, compress: options.cli.server.compress,
mime: options.cli.server.mime, mime: options.cli.server.mime,
blacklist: options.cli.server.blacklist.map(regexp => RegExp(regexp, "i")), 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) }).listen(options.cli.server.port, onServerUp)
function halt (message) { function halt (message) {
@ -53,9 +56,11 @@ function halt (message) {
} }
function onServerUp () { function onServerUp () {
const e = Date.now()
const time = `${e-s}ms`
console.error(ansi.format( console.error(ansi.format(
path.resolve(options.cli.server.directory) === process.cwd() 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}`
)) ))
} }

8
lib/local-web-server.js

@ -16,6 +16,7 @@ const _ = require('koa-route')
const mount = require('koa-mount') const mount = require('koa-mount')
const httpProxy = require('http-proxy') const httpProxy = require('http-proxy')
const pathToRegexp = require('path-to-regexp') const pathToRegexp = require('path-to-regexp')
const send = require('koa-send')
/** /**
* @module local-web-server * @module local-web-server
@ -125,6 +126,7 @@ function getApp (options) {
/* serve static files */ /* serve static files */
if (options.static.root) { if (options.static.root) {
app.use(serve(options.static.root, options.static.options)) app.use(serve(options.static.root, options.static.options))
// options.static.root.forEach(config => { // options.static.root.forEach(config => {
// app.use(mount(config.route, serve(config.root))) // app.use(mount(config.route, serve(config.root)))
// app.use(mount(config.route, serveIndex(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)) 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 return app
} }

1
package.json

@ -41,6 +41,7 @@
"koa-mount": "^1.3.0", "koa-mount": "^1.3.0",
"koa-rewrite": "^1.1.1", "koa-rewrite": "^1.1.1",
"koa-route": "^2.4.2", "koa-route": "^2.4.2",
"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",
"morgan": "^1.0.0", "morgan": "^1.0.0",

3
test/fixture/spa/.local-web-server.json

@ -0,0 +1,3 @@
{
"spa": "spa.html"
}

3
test/fixture/spa/css/style.css

@ -0,0 +1,3 @@
body {
background-color: IndianRed;
}

1
test/fixture/spa/one.html

@ -0,0 +1 @@
<h1>one</h1>

8
test/fixture/spa/spa.html

@ -0,0 +1,8 @@
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<h1>Single Page App</h1>
<h2>Location: <span></span></h2>
<script>
document.querySelector('h2 span').textContent = window.location.pathname
</script>
Loading…
Cancel
Save