Browse Source

logger common test

master
Lloyd Brookes 9 years ago
parent
commit
4edc00ca48
  1. 11
      bin/ws.js
  2. 16
      lib/local-web-server.js
  3. 1
      package.json
  4. 24
      test/test.js

11
bin/ws.js

@ -1,8 +1,17 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict' 'use strict'
const localWebServer = require('../') const localWebServer = require('../')
const streamLogStats = require('stream-log-stats')
localWebServer()
const options = {
static: { root: '.' },
serveIndex: { path: '.' },
logger: { format: 'common', options: {
stream: streamLogStats({ refreshRate: 100 })}
}
}
localWebServer(options)
.listen(8000, () => { .listen(8000, () => {
console.log(`listening`) console.log(`listening`)
}) })

16
lib/local-web-server.js

@ -2,8 +2,8 @@
const Koa = require('koa') const Koa = require('koa')
const serve = require('koa-static') const serve = require('koa-static')
const convert = require('koa-convert') const convert = require('koa-convert')
const extend = require('deep-extend')
const serveIndex = require('koa-serve-index') const serveIndex = require('koa-serve-index')
const morgan = require('koa-morgan')
/** /**
* @module local-web-server * @module local-web-server
@ -15,14 +15,22 @@ process.on('unhandledRejection', (reason, p) => {
}) })
function getApp (options) { function getApp (options) {
options = extend({
static: { root: '.' },
serveIndex: { path: '.' }
options = Object.assign({
static: {},
serveIndex: {},
logger: {}
}, options) }, options)
const app = new Koa() const app = new Koa()
if (options.logger.format) {
app.use(convert(morgan.middleware(options.logger.format, options.logger.options)))
}
if (options.static.root) {
app.use(convert(serve(options.static.root, options.static.options))) app.use(convert(serve(options.static.root, options.static.options)))
}
if (options.serveIndex.path) {
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options)))
}
return app return app
} }

1
package.json

@ -51,6 +51,7 @@
} }
}, },
"devDependencies": { "devDependencies": {
"collect-all": "^0.1.0",
"tape": "^4.2.2" "tape": "^4.2.2"
} }
} }

24
test/test.js

@ -3,6 +3,7 @@ const test = require('tape')
const request = require('req-then') const request = require('req-then')
const localWebServer = require('../') const localWebServer = require('../')
const http = require('http') const http = require('http')
const PassThrough = require('stream').PassThrough
test('static', function (t) { test('static', function (t) {
t.plan(1) t.plan(1)
@ -42,3 +43,26 @@ test('serve-index', function (t) {
}) })
.then(() => server.close()) .then(() => server.close())
}) })
test('log: common', function (t) {
t.plan(1)
const stream = PassThrough()
stream.on('readable', () => {
let chunk = stream.read()
if (chunk) t.ok(/GET/.test(chunk.toString()))
})
const app = localWebServer({
logger: {
format: 'common',
options: {
stream: stream
}
}
})
const server = http.createServer(app.callback())
server.listen(8100)
request('http://localhost:8100/')
.then(() => server.close())
})
Loading…
Cancel
Save