rewrite.. examples.. docs.. refactor
This commit is contained in:
@ -5,7 +5,7 @@ module.exports = {
|
||||
description: 'Web server port', group: 'server'
|
||||
},
|
||||
{
|
||||
name: 'log-format', alias: 'l', type: String,
|
||||
name: 'log-format', alias: 'f', type: String,
|
||||
description: "If a format is supplied an access log is written to stdout. If not, a 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'
|
||||
},
|
||||
{
|
||||
@ -17,7 +17,7 @@ module.exports = {
|
||||
description: 'Enable gzip compression, reduces bandwidth.', group: 'server'
|
||||
},
|
||||
{
|
||||
name: 'forbid', alias: 'f', type: String, multiple: true, typeLabel: '[underline]{regexp} ...',
|
||||
name: 'forbid', alias: 'b', type: String, multiple: true, typeLabel: '[underline]{regexp} ...',
|
||||
description: 'A list of forbidden routes', group: 'server'
|
||||
},
|
||||
{
|
||||
@ -25,6 +25,10 @@ module.exports = {
|
||||
description: 'Disable etag-based caching - forces loading from disk each request.', group: 'server'
|
||||
},
|
||||
{
|
||||
name: 'rewrite', alias: 'r', type: String, multiple: true, typeLabel: '[underline]{expression} ...',
|
||||
description: 'A list of URL rewrite rules', group: 'server'
|
||||
},
|
||||
{
|
||||
name: 'help', alias: 'h', type: Boolean,
|
||||
description: 'Print these usage instructions', group: 'misc'
|
||||
},
|
||||
|
@ -3,20 +3,11 @@ const path = require('path')
|
||||
const http = require('http')
|
||||
const url = require('url')
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const convert = require('koa-convert')
|
||||
const serveIndex = require('koa-serve-index')
|
||||
const morgan = require('koa-morgan')
|
||||
const compress = require('koa-compress')
|
||||
const streamLogStats = require('stream-log-stats')
|
||||
const cors = require('kcors')
|
||||
const conditional = require('koa-conditional-get');
|
||||
const etag = require('koa-etag');
|
||||
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
|
||||
@ -31,6 +22,7 @@ module.exports = localWebServer
|
||||
* @alias module:local-web-server
|
||||
* @example
|
||||
* const localWebServer = require('local-web-server')
|
||||
* localWebServer().listen(8000)
|
||||
*/
|
||||
function localWebServer (options) {
|
||||
options = Object.assign({
|
||||
@ -40,7 +32,8 @@ function localWebServer (options) {
|
||||
compress: false,
|
||||
forbid: [],
|
||||
directories: [],
|
||||
proxyRoutes: []
|
||||
proxyRoutes: [],
|
||||
rewrite: []
|
||||
}, options)
|
||||
|
||||
const log = options.log
|
||||
@ -50,33 +43,44 @@ function localWebServer (options) {
|
||||
const _use = app.use
|
||||
app.use = x => _use.call(app, convert(x))
|
||||
|
||||
const proxy = httpProxy.createProxyServer({
|
||||
changeOrigin: true
|
||||
})
|
||||
|
||||
/* Proxy routes */
|
||||
options.proxyRoutes.forEach(route => {
|
||||
app.use(_.all(route.from, function * () {
|
||||
const keys = []
|
||||
route.re = pathToRegexp(route.from, keys)
|
||||
route.new = route.to
|
||||
if (options.proxyRoutes.length) {
|
||||
const httpProxy = require('http-proxy')
|
||||
const proxy = httpProxy.createProxyServer({
|
||||
changeOrigin: true
|
||||
})
|
||||
options.proxyRoutes.forEach(route => {
|
||||
app.use(_.all(route.from, function * () {
|
||||
const keys = []
|
||||
route.re = pathToRegexp(route.from, keys)
|
||||
route.new = route.to
|
||||
|
||||
this.response = false
|
||||
keys.forEach((key, index) => {
|
||||
const re = {
|
||||
token: RegExp('\\$\\{' + key.name + '\\}', 'g'),
|
||||
index: RegExp('\\$\\{' + index + '\\}', 'g')
|
||||
}
|
||||
route.new = route.new
|
||||
.replace(re.token, arguments[index] || '')
|
||||
.replace(re.index, arguments[index] || '')
|
||||
})
|
||||
proxy.once('proxyReq', function (proxyReq) {
|
||||
proxyReq.path = url.parse(route.new).path;
|
||||
})
|
||||
proxy.web(this.req, this.res, { target: route.new })
|
||||
}))
|
||||
})
|
||||
this.response = false
|
||||
keys.forEach((key, index) => {
|
||||
const re = {
|
||||
token: RegExp('\\$\\{' + key.name + '\\}', 'g'),
|
||||
index: RegExp('\\$\\{' + index + '\\}', 'g')
|
||||
}
|
||||
route.new = route.new
|
||||
.replace(re.token, arguments[index] || '')
|
||||
.replace(re.index, arguments[index] || '')
|
||||
})
|
||||
proxy.once('proxyReq', function (proxyReq) {
|
||||
proxyReq.path = url.parse(route.new).path;
|
||||
})
|
||||
proxy.web(this.req, this.res, { target: route.new })
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
/* Rewrite rules */
|
||||
if (options.rewrite && options.rewrite.length) {
|
||||
const rewrite = require('koa-rewrite')
|
||||
options.rewrite.forEach(rule => {
|
||||
app.use(rewrite(rule.from, rule.to))
|
||||
})
|
||||
}
|
||||
|
||||
/* CORS: allow from any origin */
|
||||
app.use(cors())
|
||||
@ -94,6 +98,8 @@ function localWebServer (options) {
|
||||
|
||||
/* Cache */
|
||||
if (!options['no-cache']) {
|
||||
const conditional = require('koa-conditional-get');
|
||||
const etag = require('koa-etag');
|
||||
app.use(conditional())
|
||||
app.use(etag())
|
||||
}
|
||||
@ -113,45 +119,41 @@ function localWebServer (options) {
|
||||
|
||||
/* compress response */
|
||||
if (options.compress) {
|
||||
const compress = require('koa-compress')
|
||||
app.use(compress())
|
||||
}
|
||||
|
||||
/* special case log formats */
|
||||
if (log.format) {
|
||||
if (log.format === 'none'){
|
||||
log.format = undefined
|
||||
/* Logging */
|
||||
if (log.format !== 'none') {
|
||||
const morgan = require('koa-morgan')
|
||||
|
||||
if (!log.format) {
|
||||
const streamLogStats = require('stream-log-stats')
|
||||
log.options.stream = streamLogStats({ refreshRate: 500 })
|
||||
app.use(morgan.middleware('common', log.options))
|
||||
} else if (log.format === 'logstalgia') {
|
||||
morgan.token('date', logstalgiaDate)
|
||||
log.format = 'combined'
|
||||
app.use(morgan.middleware('combined', log.options))
|
||||
} else {
|
||||
app.use(morgan.middleware(log.format, log.options))
|
||||
}
|
||||
/* if no specific log format was requested, show log stats */
|
||||
} else {
|
||||
log.format = 'common'
|
||||
log.options.stream = streamLogStats({ refreshRate: 500 })
|
||||
}
|
||||
if (log.format) app.use(morgan.middleware(log.format, log.options))
|
||||
|
||||
// options.static.root = [
|
||||
// { route: '/one', root: 'lib' },
|
||||
// { route: '/two', root: 'node_modules' }
|
||||
// ]
|
||||
/* serve static files */
|
||||
if (options.static.root) {
|
||||
const serve = require('koa-static')
|
||||
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)))
|
||||
// })
|
||||
}
|
||||
|
||||
/* serve directory index */
|
||||
if (options.serveIndex.path) {
|
||||
const serveIndex = require('koa-serve-index')
|
||||
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) {
|
||||
const send = require('koa-send')
|
||||
app.use(_.all('*', function * () {
|
||||
yield send(this, options.spa, { root: process.cwd() })
|
||||
}))
|
||||
|
Reference in New Issue
Block a user