Browse Source

use composite, add error handler

master
Lloyd Brookes 9 years ago
parent
commit
34f245c49d
  1. 21
      bin/cli.js
  2. 69
      lib/local-web-server.js
  3. 2
      lib/middleware.js
  4. 1
      package.json

21
bin/cli.js

@ -35,7 +35,24 @@ if (options.misc.help) {
return return
} }
const app = localWebServer({
const convert = require('koa-convert')
const Koa = require('koa')
const app = new Koa()
const _use = app.use
app.use = x => _use.call(app, convert(x))
// app.use((ctx, next) => {
// return next()
// .catch(err => {
// console.error('FUKKK', err)
// })
// })
app.on('error', err => {
console.error('ERROROO', err)
})
const ws = localWebServer({
static: { static: {
root: options.server.directory, root: options.server.directory,
options: { options: {
@ -62,6 +79,8 @@ if (options.misc.help) {
mocks: options.server.mocks mocks: options.server.mocks
}) })
app.use(ws)
if (options.server.https) { if (options.server.https) {
options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key') options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt') options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')

69
lib/local-web-server.js

@ -74,15 +74,19 @@ function localWebServer (options) {
const bodyParser = require('koa-bodyparser') const bodyParser = require('koa-bodyparser')
const mw = require('./middleware') const mw = require('./middleware')
const app = new Koa()
const _use = app.use
app.use = x => _use.call(app, convert(x))
let middlewares = []
// const app = new Koa()
// const _use = app.use
// app.use = x => _use.call(app, convert(x))
/* CORS: allow from any origin */ /* CORS: allow from any origin */
app.use(cors())
// app.use(cors())
middlewares.push(cors())
/* pretty print JSON */ /* pretty print JSON */
app.use(json())
// app.use(json())
middlewares.push(json())
/* rewrite rules */ /* rewrite rules */
if (options.rewrite && options.rewrite.length) { if (options.rewrite && options.rewrite.length) {
@ -91,45 +95,53 @@ function localWebServer (options) {
/* `to` address is remote if the url specifies a host */ /* `to` address is remote if the url specifies a host */
if (url.parse(route.to).host) { if (url.parse(route.to).host) {
debug('proxy rewrite', `${route.from} -> ${route.to}`) debug('proxy rewrite', `${route.from} -> ${route.to}`)
app.use(_.all(route.from, mw.proxyRequest(route, app)))
// app.use(_.all(route.from, mw.proxyRequest(route)))
middlewares.push(_.all(route.from, mw.proxyRequest(route)))
} else { } else {
const rewrite = require('koa-rewrite') const rewrite = require('koa-rewrite')
const rmw = rewrite(route.from, route.to) const rmw = rewrite(route.from, route.to)
rmw._name = 'rewrite' rmw._name = 'rewrite'
app.use(rmw)
// app.use(rmw)
middlewares.push(rmw)
} }
} }
}) })
} }
/* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */ /* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */
app.use(bodyParser())
// app.use(bodyParser())
middlewares.push(bodyParser())
/* path blacklist */ /* path blacklist */
if (options.forbid.length) { if (options.forbid.length) {
debug('forbid', options.forbid.join(', ')) debug('forbid', options.forbid.join(', '))
app.use(mw.blacklist(options.forbid))
// app.use(mw.blacklist(options.forbid))
middlewares.push(mw.blacklist(options.forbid))
} }
/* cache */ /* cache */
if (!options['no-cache']) { if (!options['no-cache']) {
const conditional = require('koa-conditional-get') const conditional = require('koa-conditional-get')
const etag = require('koa-etag') const etag = require('koa-etag')
app.use(conditional())
app.use(etag())
// app.use(conditional())
// app.use(etag())
middlewares.push(conditional())
middlewares.push(etag())
} }
/* mime-type overrides */ /* mime-type overrides */
if (options.mime) { if (options.mime) {
debug('mime override', JSON.stringify(options.mime)) debug('mime override', JSON.stringify(options.mime))
app.use(mw.mime(options.mime))
// app.use(mw.mime(options.mime))
middlewares.push(mw.mime(options.mime))
} }
/* compress response */ /* compress response */
if (options.compress) { if (options.compress) {
const compress = require('koa-compress') const compress = require('koa-compress')
debug('compression', 'enabled') debug('compression', 'enabled')
app.use(compress())
// app.use(compress())
middlewares.push(compress())
} }
/* Logging */ /* Logging */
@ -139,12 +151,15 @@ function localWebServer (options) {
if (!log.format) { if (!log.format) {
const streamLogStats = require('stream-log-stats') const streamLogStats = require('stream-log-stats')
log.options.stream = streamLogStats({ refreshRate: 500 }) log.options.stream = streamLogStats({ refreshRate: 500 })
app.use(morgan('common', log.options))
// app.use(morgan('common', log.options))
middlewares.push(morgan('common', log.options))
} else if (log.format === 'logstalgia') { } else if (log.format === 'logstalgia') {
morgan.token('date', logstalgiaDate) morgan.token('date', logstalgiaDate)
app.use(morgan('combined', log.options))
// app.use(morgan('combined', log.options))
middlewares.push(morgan('combined', log.options))
} else { } else {
app.use(morgan(log.format, log.options))
// app.use(morgan(log.format, log.options))
middlewares.push(morgan(log.format, log.options))
} }
} }
@ -155,13 +170,15 @@ function localWebServer (options) {
} }
if (mock.responses) { if (mock.responses) {
app.use(mw.mockResponses(mock.route, mock.responses))
// app.use(mw.mockResponses(mock.route, mock.responses))
middlewares.push(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
} }
app.use(mw.mockResponses(mock.route, mock.target))
// app.use(mw.mockResponses(mock.route, mock.target))
middlewares.push(mw.mockResponses(mock.route, mock.target))
} }
}) })
@ -169,7 +186,11 @@ function localWebServer (options) {
if (options.spa) { if (options.spa) {
const historyApiFallback = require('koa-connect-history-api-fallback') const historyApiFallback = require('koa-connect-history-api-fallback')
debug('SPA', options.spa) debug('SPA', options.spa)
app.use(historyApiFallback({
// app.use(historyApiFallback({
// index: options.spa,
// verbose: options.verbose
// }))
middlewares.push(historyApiFallback({
index: options.spa, index: options.spa,
verbose: options.verbose verbose: options.verbose
})) }))
@ -178,16 +199,20 @@ function localWebServer (options) {
/* serve static files */ /* serve static files */
if (options.static.root) { if (options.static.root) {
const serve = require('koa-static') const serve = require('koa-static')
app.use(serve(options.static.root, options.static.options))
// app.use(serve(options.static.root, options.static.options))
middlewares.push(serve(options.static.root, options.static.options))
} }
/* serve directory index */ /* serve directory index */
if (options.serveIndex.path) { if (options.serveIndex.path) {
const serveIndex = require('koa-serve-index') const serveIndex = require('koa-serve-index')
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
// app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
middlewares.push(serveIndex(options.serveIndex.path, options.serveIndex.options))
} }
return app
const compose = require('koa-compose')
middlewares = middlewares.map(convert)
return compose(middlewares)
} }
function logstalgiaDate () { function logstalgiaDate () {

2
lib/middleware.js

@ -15,7 +15,7 @@ exports.blacklist = blacklist
exports.mockResponses = mockResponses exports.mockResponses = mockResponses
exports.mime = mime exports.mime = mime
function proxyRequest (route, app) {
function proxyRequest (route) {
const httpProxy = require('http-proxy') const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer({ const proxy = httpProxy.createProxyServer({
changeOrigin: true changeOrigin: true

1
package.json

@ -38,6 +38,7 @@
"kcors": "^1.2.0", "kcors": "^1.2.0",
"koa": "^2.0.0", "koa": "^2.0.0",
"koa-bodyparser": "^3.0.0", "koa-bodyparser": "^3.0.0",
"koa-compose": "^3.1.0",
"koa-compress": "^1.0.9", "koa-compress": "^1.0.9",
"koa-conditional-get": "^1.0.3", "koa-conditional-get": "^1.0.3",
"koa-connect-history-api-fallback": "^0.3.0", "koa-connect-history-api-fallback": "^0.3.0",

Loading…
Cancel
Save