refactor
This commit is contained in:
@ -8,7 +8,7 @@
|
|||||||
***Requires node v4.0.0 or higher. Install the [previous release](https://github.com/75lb/local-web-server/tree/prev) for older node support.***
|
***Requires node v4.0.0 or higher. Install the [previous release](https://github.com/75lb/local-web-server/tree/prev) for older node support.***
|
||||||
|
|
||||||
# local-web-server
|
# local-web-server
|
||||||
A simple web-server for productive front-end development. Typical use cases:
|
A simple, extensible web-server for productive front-end development. Typical use cases:
|
||||||
|
|
||||||
* Front-end Development
|
* Front-end Development
|
||||||
* Static or Single Page App development
|
* Static or Single Page App development
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
const LocalWebServer = require('../')
|
const LocalWebServer = require('../')
|
||||||
|
|
||||||
const ws = new LocalWebServer()
|
const ws = new LocalWebServer()
|
||||||
ws.middleware
|
ws.addCors()
|
||||||
.addCors()
|
|
||||||
.addJson()
|
.addJson()
|
||||||
.addRewrite()
|
.addRewrite()
|
||||||
.addBodyParser()
|
.addBodyParser()
|
||||||
@ -17,4 +16,4 @@ ws.middleware
|
|||||||
.addSpa()
|
.addSpa()
|
||||||
.addStatic()
|
.addStatic()
|
||||||
.addIndex()
|
.addIndex()
|
||||||
ws.listen()
|
.start()
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const LocalWebServer = require('../')
|
const LocalWebServer = require('../')
|
||||||
const cacheControl = require('koa-cache-control')
|
const cacheControl = require('koa-cache-control')
|
||||||
const cliData = require('../lib/cli-data')
|
|
||||||
|
|
||||||
cliData.optionDefinitions.push({ name: 'maxage', group: 'misc' })
|
const optionDefinitions = { name: 'maxage', type: Number, defaultValue: 1000 }
|
||||||
|
|
||||||
const ws = new LocalWebServer()
|
const ws = new LocalWebServer()
|
||||||
ws.middleware
|
ws.addLogging('dev')
|
||||||
.addLogging('dev')
|
.add({
|
||||||
.add(cacheControl({
|
optionDefinitions: optionDefinitions,
|
||||||
maxAge: 15
|
middleware: function (options) {
|
||||||
}))
|
return cacheControl({ maxAge: options.middleware.maxage })
|
||||||
|
}
|
||||||
|
})
|
||||||
.addStatic()
|
.addStatic()
|
||||||
.addIndex()
|
.addIndex()
|
||||||
ws.listen()
|
.start()
|
||||||
|
10
extend/index.html
Normal file
10
extend/index.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>live-reload demo</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Live reloaded attached</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -3,8 +3,7 @@ const Cli = require('../')
|
|||||||
const liveReload = require('koa-livereload')
|
const liveReload = require('koa-livereload')
|
||||||
|
|
||||||
const ws = new Cli()
|
const ws = new Cli()
|
||||||
ws.middleware
|
ws.addLogging('dev')
|
||||||
.addLogging('dev')
|
.add({ middleware: liveReload })
|
||||||
.add(liveReload())
|
|
||||||
.addStatic()
|
.addStatic()
|
||||||
ws.listen(8000)
|
.start()
|
||||||
|
@ -4,34 +4,6 @@ exports.optionDefinitions = [
|
|||||||
description: 'Web server port.', group: 'server'
|
description: 'Web server port.', group: 'server'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'directory', alias: 'd', type: String, typeLabel: '[underline]{path}',
|
|
||||||
description: 'Root directory, defaults to the current directory.', group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'log-format', alias: 'f', type: String,
|
|
||||||
description: "If a format is supplied an access log is written to stdout. If not, a dynamic statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url').", group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'rewrite', alias: 'r', type: String, multiple: true, typeLabel: '[underline]{expression} ...',
|
|
||||||
description: "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'.", group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'spa', alias: 's', type: String, typeLabel: '[underline]{file}',
|
|
||||||
description: 'Path to a Single Page App, e.g. app.html.', group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'compress', alias: 'c', type: Boolean,
|
|
||||||
description: 'Serve gzip-compressed resources, where applicable.', group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'forbid', alias: 'b', type: String, multiple: true, typeLabel: '[underline]{path} ...',
|
|
||||||
description: 'A list of forbidden routes.', group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'no-cache', alias: 'n', type: Boolean,
|
|
||||||
description: 'Disable etag-based caching - forces loading from disk each request.', group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'key', type: String, typeLabel: '[underline]{file}', group: 'server',
|
name: 'key', type: String, typeLabel: '[underline]{file}', group: 'server',
|
||||||
description: 'SSL key. Supply along with --cert to launch a https server.'
|
description: 'SSL key. Supply along with --cert to launch a https server.'
|
||||||
},
|
},
|
||||||
@ -44,43 +16,52 @@ exports.optionDefinitions = [
|
|||||||
description: 'Enable HTTPS using a built-in key and cert, registered to the domain 127.0.0.1.'
|
description: 'Enable HTTPS using a built-in key and cert, registered to the domain 127.0.0.1.'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'verbose', type: Boolean,
|
|
||||||
description: 'Verbose output, useful for debugging.', group: 'server'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'help', alias: 'h', type: Boolean,
|
name: 'help', alias: 'h', type: Boolean,
|
||||||
description: 'Print these usage instructions.', group: 'misc'
|
description: 'Print these usage instructions.', group: 'misc'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'config', type: Boolean,
|
name: 'config', type: Boolean,
|
||||||
description: 'Print the stored config.', group: 'misc'
|
description: 'Print the stored config.', group: 'misc'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'verbose', type: Boolean,
|
||||||
|
description: 'Verbose output, useful for debugging.', group: 'misc'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
exports.usage = [
|
function usage (middlewareDefinitions) {
|
||||||
{
|
return [
|
||||||
header: 'local-web-server',
|
{
|
||||||
content: 'A simple web-server for productive front-end development.'
|
header: 'local-web-server',
|
||||||
},
|
content: 'A simple web-server for productive front-end development.'
|
||||||
{
|
},
|
||||||
header: 'Synopsis',
|
{
|
||||||
content: [
|
header: 'Synopsis',
|
||||||
'$ ws [<server options>]',
|
content: [
|
||||||
'$ ws --config',
|
'$ ws [<server options>]',
|
||||||
'$ ws --help'
|
'$ ws --config',
|
||||||
]
|
'$ ws --help'
|
||||||
},
|
]
|
||||||
{
|
},
|
||||||
header: 'Server options',
|
{
|
||||||
optionList: exports.optionDefinitions,
|
header: 'Server',
|
||||||
group: 'server'
|
optionList: exports.optionDefinitions,
|
||||||
},
|
group: 'server'
|
||||||
{
|
},
|
||||||
header: 'Misc options',
|
{
|
||||||
optionList: exports.optionDefinitions,
|
header: 'Middleware',
|
||||||
group: 'misc'
|
optionList: middlewareDefinitions,
|
||||||
},
|
group: 'middleware'
|
||||||
{
|
},
|
||||||
content: 'Project home: [underline]{https://github.com/75lb/local-web-server}'
|
{
|
||||||
}
|
header: 'Misc',
|
||||||
]
|
optionList: exports.optionDefinitions,
|
||||||
|
group: 'misc'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
content: 'Project home: [underline]{https://github.com/75lb/local-web-server}'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.usage = usage
|
||||||
|
@ -5,56 +5,53 @@ const path = require('path')
|
|||||||
const arrayify = require('array-back')
|
const arrayify = require('array-back')
|
||||||
const t = require('typical')
|
const t = require('typical')
|
||||||
const Tool = require('command-line-tool')
|
const Tool = require('command-line-tool')
|
||||||
|
const MiddlewareStack = require('./middleware-stack')
|
||||||
|
|
||||||
const tool = new Tool()
|
const tool = new Tool()
|
||||||
|
|
||||||
class Cli {
|
class Cli extends MiddlewareStack {
|
||||||
constructor () {
|
start () {
|
||||||
this.options = collectOptions()
|
const options = collectOptions(this.getOptionDefinitions())
|
||||||
this.app = null
|
this.options = options
|
||||||
this.middleware = null
|
|
||||||
|
|
||||||
const options = this.options
|
if (options.misc.verbose) {
|
||||||
|
process.env.DEBUG = '*'
|
||||||
|
}
|
||||||
|
|
||||||
if (options.misc.config) {
|
if (options.misc.config) {
|
||||||
tool.stop(JSON.stringify(options.server, null, ' '), 0)
|
tool.stop(JSON.stringify(options, null, ' '), 0)
|
||||||
} else {
|
} else {
|
||||||
const Koa = require('koa')
|
const Koa = require('koa')
|
||||||
const app = new Koa()
|
const app = new Koa()
|
||||||
app.on('error', err => {
|
app.on('error', err => {
|
||||||
if (options.server['log-format']) {
|
if (options.middleware['log-format']) {
|
||||||
console.error(ansi.format(err.message, 'red'))
|
console.error(ansi.format(err.message, 'red'))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.app = app
|
|
||||||
|
|
||||||
const MiddlewareStack = require('./middleware-stack')
|
app.use(this.compose(options))
|
||||||
this.middleware = new MiddlewareStack(options)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
listen () {
|
let key = options.server.key
|
||||||
this.app.use(this.middleware.compose())
|
let cert = options.server.cert
|
||||||
const options = this.options
|
if (options.server.https) {
|
||||||
const key = this.options.server.key
|
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
|
||||||
const cert = this.options.server.cert
|
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
|
||||||
if (options.server.https) {
|
|
||||||
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
|
|
||||||
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key && cert) {
|
|
||||||
const https = require('https')
|
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const serverOptions = {
|
|
||||||
key: fs.readFileSync(key),
|
|
||||||
cert: fs.readFileSync(cert)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = https.createServer(serverOptions, this.app.callback())
|
if (key && cert) {
|
||||||
server.listen(options.server.port, onServerUp.bind(null, options, true))
|
const https = require('https')
|
||||||
} else {
|
const fs = require('fs')
|
||||||
this.app.listen(options.server.port, onServerUp.bind(null, options))
|
|
||||||
|
const serverOptions = {
|
||||||
|
key: fs.readFileSync(key),
|
||||||
|
cert: fs.readFileSync(cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
const server = https.createServer(serverOptions, app.callback())
|
||||||
|
server.listen(options.server.port, onServerUp.bind(null, options, true))
|
||||||
|
} else {
|
||||||
|
app.listen(options.server.port, onServerUp.bind(null, options))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,9 +62,9 @@ function onServerUp (options, isHttps) {
|
|||||||
.join(', ')
|
.join(', ')
|
||||||
|
|
||||||
console.error(ansi.format(
|
console.error(ansi.format(
|
||||||
path.resolve(options.server.directory) === process.cwd()
|
path.resolve(options.middleware.directory) === process.cwd()
|
||||||
? `serving at ${ipList}`
|
? `serving at ${ipList}`
|
||||||
: `serving [underline]{${options.server.directory}} at ${ipList}`
|
: `serving [underline]{${options.middleware.directory}} at ${ipList}`
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,36 +83,38 @@ function getIPList (isHttps) {
|
|||||||
/**
|
/**
|
||||||
* Return default, stored and command-line options combined
|
* Return default, stored and command-line options combined
|
||||||
*/
|
*/
|
||||||
function collectOptions () {
|
function collectOptions (mwOptionDefinitions) {
|
||||||
const loadConfig = require('config-master')
|
const loadConfig = require('config-master')
|
||||||
const stored = loadConfig('local-web-server')
|
const stored = loadConfig('local-web-server')
|
||||||
const cli = require('../lib/cli-data')
|
const cli = require('../lib/cli-data')
|
||||||
|
|
||||||
/* parse command line args */
|
/* parse command line args */
|
||||||
let options = tool.getOptions(cli.optionDefinitions, cli.usage)
|
const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
|
||||||
|
let options = tool.getOptions(definitions, cli.usage(definitions))
|
||||||
const builtIn = {
|
|
||||||
port: 8000,
|
|
||||||
directory: process.cwd()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.server.rewrite) {
|
|
||||||
options.server.rewrite = parseRewriteRules(options.server.rewrite)
|
|
||||||
}
|
|
||||||
|
|
||||||
/* override built-in defaults with stored config and then command line args */
|
/* override built-in defaults with stored config and then command line args */
|
||||||
options.server = Object.assign(builtIn, stored, options.server)
|
options.server = Object.assign({ port: 8000 }, stored.server, options.server)
|
||||||
|
options.middleware = Object.assign({ directory: process.cwd() }, stored.middleware || {}, options.middleware)
|
||||||
|
|
||||||
|
if (options.middleware.rewrite) {
|
||||||
|
options.middleware.rewrite = parseRewriteRules(options.middleware.rewrite)
|
||||||
|
}
|
||||||
|
|
||||||
validateOptions(options)
|
validateOptions(options)
|
||||||
|
// console.log(options)
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseRewriteRules (rules) {
|
function parseRewriteRules (rules) {
|
||||||
return rules && rules.map(rule => {
|
return rules && rules.map(rule => {
|
||||||
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
|
if (t.isString(rule)) {
|
||||||
return {
|
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
|
||||||
from: matches[1],
|
return {
|
||||||
to: matches[2]
|
from: matches[1],
|
||||||
|
to: matches[2]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return rule
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -5,17 +5,9 @@ const url = require('url')
|
|||||||
const debug = require('debug')('local-web-server')
|
const debug = require('debug')('local-web-server')
|
||||||
const mw = require('./middleware')
|
const mw = require('./middleware')
|
||||||
const t = require('typical')
|
const t = require('typical')
|
||||||
|
const compose = require('koa-compose')
|
||||||
|
|
||||||
class MiddlewareStack extends Array {
|
class MiddlewareStack extends Array {
|
||||||
constructor (options) {
|
|
||||||
super()
|
|
||||||
this.options = options
|
|
||||||
|
|
||||||
if (options.verbose) {
|
|
||||||
process.env.DEBUG = '*'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
add (middleware) {
|
add (middleware) {
|
||||||
this.push(middleware)
|
this.push(middleware)
|
||||||
return this
|
return this
|
||||||
@ -25,141 +17,196 @@ class MiddlewareStack extends Array {
|
|||||||
* allow from any origin
|
* allow from any origin
|
||||||
*/
|
*/
|
||||||
addCors () {
|
addCors () {
|
||||||
this.push(require('kcors')())
|
this.push({ middleware: require('kcors') })
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* pretty print JSON */
|
/* pretty print JSON */
|
||||||
addJson () {
|
addJson () {
|
||||||
this.push(require('koa-json')())
|
this.push({ middleware: require('koa-json') })
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* rewrite rules */
|
/* rewrite rules */
|
||||||
addRewrite (rewriteRules) {
|
addRewrite (rewriteRules) {
|
||||||
const options = arrayify(this.options.server.rewrite || rewriteRules)
|
this.push({
|
||||||
if (options.length) {
|
optionDefinitions: {
|
||||||
options.forEach(route => {
|
name: 'rewrite', alias: 'r', type: String, multiple: true,
|
||||||
if (route.to) {
|
typeLabel: '[underline]{expression} ...',
|
||||||
/* `to` address is remote if the url specifies a host */
|
description: "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'."
|
||||||
if (url.parse(route.to).host) {
|
},
|
||||||
const _ = require('koa-route')
|
middleware: function (cliOptions) {
|
||||||
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
const options = arrayify(cliOptions.middleware.rewrite || rewriteRules)
|
||||||
this.push(_.all(route.from, mw.proxyRequest(route)))
|
if (options.length) {
|
||||||
} else {
|
options.forEach(route => {
|
||||||
const rewrite = require('koa-rewrite')
|
if (route.to) {
|
||||||
const rmw = rewrite(route.from, route.to)
|
/* `to` address is remote if the url specifies a host */
|
||||||
rmw._name = 'rewrite'
|
if (url.parse(route.to).host) {
|
||||||
this.push(rmw)
|
const _ = require('koa-route')
|
||||||
}
|
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
||||||
|
return _.all(route.from, mw.proxyRequest(route))
|
||||||
|
} else {
|
||||||
|
const rewrite = require('koa-rewrite')
|
||||||
|
const rmw = rewrite(route.from, route.to)
|
||||||
|
rmw._name = 'rewrite'
|
||||||
|
return rmw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* must come after rewrite.
|
/* must come after rewrite.
|
||||||
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
||||||
addBodyParser () {
|
addBodyParser () {
|
||||||
this.push(require('koa-bodyparser')())
|
this.push({ middleware: require('koa-bodyparser') })
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* path blacklist */
|
/* path blacklist */
|
||||||
addBlacklist (forbidList) {
|
addBlacklist (forbidList) {
|
||||||
forbidList = arrayify(this.options.server.forbid || forbidList)
|
this.push({
|
||||||
if (forbidList.length) {
|
optionDefinitions: {
|
||||||
const pathToRegexp = require('path-to-regexp')
|
name: 'forbid', alias: 'b', type: String,
|
||||||
debug('forbid', forbidList.join(', '))
|
multiple: true, typeLabel: '[underline]{path} ...',
|
||||||
this.push(function blacklist (ctx, next) {
|
description: 'A list of forbidden routes.'
|
||||||
if (forbidList.some(expression => pathToRegexp(expression).test(ctx.path))) {
|
},
|
||||||
ctx.throw(403, http.STATUS_CODES[403])
|
middleware: function (cliOptions) {
|
||||||
} else {
|
forbidList = arrayify(cliOptions.middleware.forbid || forbidList)
|
||||||
return next()
|
if (forbidList.length) {
|
||||||
|
const pathToRegexp = require('path-to-regexp')
|
||||||
|
debug('forbid', forbidList.join(', '))
|
||||||
|
return function blacklist (ctx, next) {
|
||||||
|
if (forbidList.some(expression => pathToRegexp(expression).test(ctx.path))) {
|
||||||
|
const http = require('http')
|
||||||
|
ctx.throw(403, http.STATUS_CODES[403])
|
||||||
|
} else {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cache */
|
/* cache */
|
||||||
addCache () {
|
addCache () {
|
||||||
const noCache = this.options.server['no-cache']
|
this.push({
|
||||||
if (!noCache) {
|
optionDefinitions: {
|
||||||
this.push(require('koa-conditional-get')())
|
name: 'no-cache', alias: 'n', type: Boolean,
|
||||||
this.push(require('koa-etag')())
|
description: 'Disable etag-based caching - forces loading from disk each request.'
|
||||||
}
|
},
|
||||||
|
middleware: function (cliOptions) {
|
||||||
|
const noCache = cliOptions.middleware['no-cache']
|
||||||
|
if (!noCache) {
|
||||||
|
return [
|
||||||
|
require('koa-conditional-get')(),
|
||||||
|
require('koa-etag')()
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mime-type overrides */
|
/* mime-type overrides */
|
||||||
addMimeType (mime) {
|
addMimeType (mime) {
|
||||||
mime = this.options.server.mime || mime
|
this.push({
|
||||||
if (mime) {
|
middleware: function (cliOptions) {
|
||||||
debug('mime override', JSON.stringify(mime))
|
mime = cliOptions.middleware.mime || mime
|
||||||
this.push(mw.mime(mime))
|
if (mime) {
|
||||||
}
|
debug('mime override', JSON.stringify(mime))
|
||||||
|
return mw.mime(mime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compress response */
|
/* compress response */
|
||||||
addCompression (compress) {
|
addCompression (compress) {
|
||||||
compress = t.isDefined(this.options.server.compress)
|
this.push({
|
||||||
? this.options.server.compress
|
optionDefinitions: {
|
||||||
: compress
|
name: 'compress', alias: 'c', type: Boolean,
|
||||||
if (compress) {
|
description: 'Serve gzip-compressed resources, where applicable.'
|
||||||
debug('compression', 'enabled')
|
},
|
||||||
this.push(require('koa-compress')())
|
middleware: function (cliOptions) {
|
||||||
}
|
compress = t.isDefined(cliOptions.middleware.compress)
|
||||||
|
? cliOptions.middleware.compress
|
||||||
|
: compress
|
||||||
|
if (compress) {
|
||||||
|
debug('compression', 'enabled')
|
||||||
|
return require('koa-compress')()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Logging */
|
/* Logging */
|
||||||
addLogging (format, options) {
|
addLogging (format, options) {
|
||||||
format = this.options.server['log-format'] || format
|
|
||||||
options = options || {}
|
options = options || {}
|
||||||
|
this.push({
|
||||||
|
optionDefinitions: {
|
||||||
|
name: 'log-format',
|
||||||
|
alias: 'f',
|
||||||
|
type: String,
|
||||||
|
description: "If a format is supplied an access log is written to stdout. If not, a dynamic statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url')."
|
||||||
|
},
|
||||||
|
middleware: function (cliOptions) {
|
||||||
|
format = cliOptions.middleware['log-format'] || format
|
||||||
|
|
||||||
if (this.options.verbose && !format) {
|
if (cliOptions.misc.verbose && !format) {
|
||||||
format = 'none'
|
format = 'none'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format !== 'none') {
|
if (format !== 'none') {
|
||||||
const morgan = require('koa-morgan')
|
const morgan = require('koa-morgan')
|
||||||
|
|
||||||
if (!format) {
|
if (!format) {
|
||||||
const streamLogStats = require('stream-log-stats')
|
const streamLogStats = require('stream-log-stats')
|
||||||
options.stream = streamLogStats({ refreshRate: 500 })
|
options.stream = streamLogStats({ refreshRate: 500 })
|
||||||
this.push(morgan('common', options))
|
return morgan('common', options)
|
||||||
} else if (format === 'logstalgia') {
|
} else if (format === 'logstalgia') {
|
||||||
morgan.token('date', () => {
|
morgan.token('date', () => {
|
||||||
var d = new Date()
|
var d = new Date()
|
||||||
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
|
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
|
||||||
})
|
})
|
||||||
this.push(morgan('combined', options))
|
return morgan('combined', options)
|
||||||
} else {
|
} else {
|
||||||
this.push(morgan(format, options))
|
return morgan(format, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Mock Responses */
|
/* Mock Responses */
|
||||||
addMockResponses (mocks) {
|
addMockResponses (mocks) {
|
||||||
mocks = arrayify(this.options.server.mocks || mocks)
|
this.push({
|
||||||
mocks.forEach(mock => {
|
middleware: function (cliOptions) {
|
||||||
if (mock.module) {
|
mocks = arrayify(cliOptions.middleware.mocks || mocks)
|
||||||
// TODO: ENSURE this.options.static.root is correct value
|
mocks.forEach(mock => {
|
||||||
mock.responses = require(path.resolve(path.join(this.options.static.root, mock.module)))
|
if (mock.module) {
|
||||||
}
|
// TODO: ENSURE cliOptions.static.root is correct value
|
||||||
|
mock.responses = require(path.resolve(path.join(cliOptions.static.root, mock.module)))
|
||||||
|
}
|
||||||
|
|
||||||
if (mock.responses) {
|
if (mock.responses) {
|
||||||
this.push(mw.mockResponses(mock.route, mock.responses))
|
return mw.mockResponses(mock.route, mock.responses)
|
||||||
} else if (mock.response) {
|
} else if (mock.response) {
|
||||||
mock.target = {
|
mock.target = {
|
||||||
request: mock.request,
|
request: mock.request,
|
||||||
response: mock.response
|
response: mock.response
|
||||||
}
|
}
|
||||||
this.push(mw.mockResponses(mock.route, mock.target))
|
return mw.mockResponses(mock.route, mock.target)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return this
|
return this
|
||||||
@ -167,44 +214,77 @@ class MiddlewareStack extends Array {
|
|||||||
|
|
||||||
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
|
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
|
||||||
addSpa (spa) {
|
addSpa (spa) {
|
||||||
spa = t.isDefined(this.options.server.spa) ? this.options.server.spa : spa
|
this.push({
|
||||||
if (spa) {
|
optionDefinitions: {
|
||||||
const historyApiFallback = require('koa-connect-history-api-fallback')
|
name: 'spa', alias: 's', type: String, typeLabel: '[underline]{file}',
|
||||||
debug('SPA', spa)
|
description: 'Path to a Single Page App, e.g. app.html.'
|
||||||
this.push(historyApiFallback({
|
},
|
||||||
index: spa,
|
middleware: function (cliOptions) {
|
||||||
verbose: this.options.verbose
|
spa = t.isDefined(cliOptions.middleware.spa) ? cliOptions.middleware.spa : spa
|
||||||
}))
|
if (spa) {
|
||||||
}
|
const historyApiFallback = require('koa-connect-history-api-fallback')
|
||||||
|
debug('SPA', spa)
|
||||||
|
return historyApiFallback({
|
||||||
|
index: spa,
|
||||||
|
verbose: cliOptions.misc.verbose
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* serve static files */
|
/* serve static files */
|
||||||
addStatic (root, options) {
|
addStatic (root, options) {
|
||||||
root = this.options.server.directory || root || process.cwd()
|
this.push({
|
||||||
options = Object.assign({ hidden: true }, options)
|
optionDefinitions: {
|
||||||
if (root) {
|
name: 'directory', alias: 'd', type: String, typeLabel: '[underline]{path}',
|
||||||
const serve = require('koa-static')
|
description: 'Root directory, defaults to the current directory.'
|
||||||
this.push(serve(root, options))
|
},
|
||||||
}
|
middleware: function (cliOptions) {
|
||||||
|
root = cliOptions.middleware.directory || root || process.cwd()
|
||||||
|
options = Object.assign({ hidden: true }, options)
|
||||||
|
// console.log(root, options, cliOptions)
|
||||||
|
if (root) {
|
||||||
|
const serve = require('koa-static')
|
||||||
|
return serve(root, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* serve directory index */
|
/* serve directory index */
|
||||||
addIndex (path, options) {
|
addIndex (path, options) {
|
||||||
path = this.options.server.directory || path || process.cwd()
|
this.push({
|
||||||
options = Object.assign({ icons: true, hidden: true }, options)
|
middleware: function (cliOptions) {
|
||||||
if (path) {
|
path = cliOptions.middleware.directory || path || process.cwd()
|
||||||
const serveIndex = require('koa-serve-index')
|
options = Object.assign({ icons: true, hidden: true }, options)
|
||||||
this.push(serveIndex(path, options))
|
if (path) {
|
||||||
}
|
const serveIndex = require('koa-serve-index')
|
||||||
|
return serveIndex(path, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOptionDefinitions () {
|
||||||
|
const flatten = require('reduce-flatten')
|
||||||
|
return this
|
||||||
|
.filter(mw => mw.optionDefinitions)
|
||||||
|
.map(mw => mw.optionDefinitions)
|
||||||
|
.reduce(flatten, [])
|
||||||
|
.map(def => {
|
||||||
|
def.group = 'middleware'
|
||||||
|
return def
|
||||||
|
})
|
||||||
|
}
|
||||||
compose (options) {
|
compose (options) {
|
||||||
const compose = require('koa-compose')
|
|
||||||
const convert = require('koa-convert')
|
const convert = require('koa-convert')
|
||||||
const middlewareStack = this.map(convert)
|
const middlewareStack = this
|
||||||
|
.filter(mw => mw)
|
||||||
|
.map(mw => compose(arrayify(mw.middleware(options)).map(convert)))
|
||||||
return compose(middlewareStack)
|
return compose(middlewareStack)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const http = require('http')
|
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
const arrayify = require('array-back')
|
const arrayify = require('array-back')
|
||||||
const t = require('typical')
|
const t = require('typical')
|
||||||
@ -11,7 +10,6 @@ const debug = require('debug')('local-web-server')
|
|||||||
* @module middleware
|
* @module middleware
|
||||||
*/
|
*/
|
||||||
exports.proxyRequest = proxyRequest
|
exports.proxyRequest = proxyRequest
|
||||||
exports.blacklist = blacklist
|
|
||||||
exports.mockResponses = mockResponses
|
exports.mockResponses = mockResponses
|
||||||
exports.mime = mime
|
exports.mime = mime
|
||||||
|
|
||||||
@ -49,16 +47,6 @@ function proxyRequest (route) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function blacklist (forbid) {
|
|
||||||
return function blacklist (ctx, next) {
|
|
||||||
if (forbid.some(expression => pathToRegexp(expression).test(ctx.path))) {
|
|
||||||
ctx.throw(403, http.STATUS_CODES[403])
|
|
||||||
} else {
|
|
||||||
return next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function mime (mimeTypes) {
|
function mime (mimeTypes) {
|
||||||
return function mime (ctx, next) {
|
return function mime (ctx, next) {
|
||||||
return next().then(() => {
|
return next().then(() => {
|
||||||
|
Reference in New Issue
Block a user