SPA option added

This commit is contained in:
Lloyd Brookes
2015-11-15 14:53:25 +00:00
parent b42e5f3918
commit 99305cb78a
7 changed files with 32 additions and 3 deletions

View File

@ -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}`
))
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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