logger common test
This commit is contained in:
11
bin/ws.js
11
bin/ws.js
@ -1,8 +1,17 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict'
|
||||
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, () => {
|
||||
console.log(`listening`)
|
||||
})
|
||||
|
@ -2,8 +2,8 @@
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const convert = require('koa-convert')
|
||||
const extend = require('deep-extend')
|
||||
const serveIndex = require('koa-serve-index')
|
||||
const morgan = require('koa-morgan')
|
||||
|
||||
/**
|
||||
* @module local-web-server
|
||||
@ -15,14 +15,22 @@ process.on('unhandledRejection', (reason, p) => {
|
||||
})
|
||||
|
||||
function getApp (options) {
|
||||
options = extend({
|
||||
static: { root: '.' },
|
||||
serveIndex: { path: '.' }
|
||||
options = Object.assign({
|
||||
static: {},
|
||||
serveIndex: {},
|
||||
logger: {}
|
||||
}, options)
|
||||
|
||||
const app = new Koa()
|
||||
|
||||
app.use(convert(serve(options.static.root, options.static.options)))
|
||||
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options)))
|
||||
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)))
|
||||
}
|
||||
if (options.serveIndex.path) {
|
||||
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options)))
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
@ -51,6 +51,7 @@
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"collect-all": "^0.1.0",
|
||||
"tape": "^4.2.2"
|
||||
}
|
||||
}
|
||||
|
24
test/test.js
24
test/test.js
@ -3,6 +3,7 @@ const test = require('tape')
|
||||
const request = require('req-then')
|
||||
const localWebServer = require('../')
|
||||
const http = require('http')
|
||||
const PassThrough = require('stream').PassThrough
|
||||
|
||||
test('static', function (t) {
|
||||
t.plan(1)
|
||||
@ -42,3 +43,26 @@ test('serve-index', function (t) {
|
||||
})
|
||||
.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())
|
||||
})
|
||||
|
Reference in New Issue
Block a user