SPA option added
This commit is contained in:
11
bin/cli.js
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 at [underline]{http://localhost:${options.cli.server.port}} ${time}`
|
||||||
: `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}`
|
: `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}} ${time}`
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
Normal file
3
test/fixture/spa/.local-web-server.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"spa": "spa.html"
|
||||||
|
}
|
3
test/fixture/spa/css/style.css
Normal file
3
test/fixture/spa/css/style.css
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
body {
|
||||||
|
background-color: IndianRed;
|
||||||
|
}
|
1
test/fixture/spa/one.html
Normal file
1
test/fixture/spa/one.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<h1>one</h1>
|
8
test/fixture/spa/spa.html
Normal file
8
test/fixture/spa/spa.html
Normal 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>
|
Reference in New Issue
Block a user