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
}
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: {
root: options.server.directory,
options: {
@ -62,6 +79,8 @@ if (options.misc.help) {
mocks: options.server.mocks
})
app.use(ws)
if (options.server.https) {
options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
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 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 */
app.use(cors())
// app.use(cors())
middlewares.push(cors())
/* pretty print JSON */
app.use(json())
// app.use(json())
middlewares.push(json())
/* rewrite rules */
if (options.rewrite && options.rewrite.length) {
@ -91,45 +95,53 @@ function localWebServer (options) {
/* `to` address is remote if the url specifies a host */
if (url.parse(route.to).host) {
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 {
const rewrite = require('koa-rewrite')
const rmw = rewrite(route.from, route.to)
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. */
app.use(bodyParser())
// app.use(bodyParser())
middlewares.push(bodyParser())
/* path blacklist */
if (options.forbid.length) {
debug('forbid', options.forbid.join(', '))
app.use(mw.blacklist(options.forbid))
// app.use(mw.blacklist(options.forbid))
middlewares.push(mw.blacklist(options.forbid))
}
/* cache */
if (!options['no-cache']) {
const conditional = require('koa-conditional-get')
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 */
if (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 */
if (options.compress) {
const compress = require('koa-compress')
debug('compression', 'enabled')
app.use(compress())
// app.use(compress())
middlewares.push(compress())
}
/* Logging */
@ -139,12 +151,15 @@ function localWebServer (options) {
if (!log.format) {
const streamLogStats = require('stream-log-stats')
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') {
morgan.token('date', logstalgiaDate)
app.use(morgan('combined', log.options))
// app.use(morgan('combined', log.options))
middlewares.push(morgan('combined', log.options))
} 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) {
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) {
mock.target = {
request: mock.request,
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) {
const historyApiFallback = require('koa-connect-history-api-fallback')
debug('SPA', options.spa)
app.use(historyApiFallback({
// app.use(historyApiFallback({
// index: options.spa,
// verbose: options.verbose
// }))
middlewares.push(historyApiFallback({
index: options.spa,
verbose: options.verbose
}))
@ -178,16 +199,20 @@ function localWebServer (options) {
/* serve static files */
if (options.static.root) {
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 */
if (options.serveIndex.path) {
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 () {

2
lib/middleware.js

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

1
package.json

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

Loading…
Cancel
Save