Browse Source

logging, compress

master
Lloyd Brookes 9 years ago
parent
commit
ffc39145ee
  1. 19
      bin/ws.js
  2. 34
      lib/local-web-server.js
  3. 1
      package.json
  4. 13
      test/test.js

19
bin/ws.js

@ -1,7 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict' 'use strict'
const localWebServer = require('../') const localWebServer = require('../')
const streamLogStats = require('stream-log-stats')
const commandLineArgs = require('command-line-args') const commandLineArgs = require('command-line-args')
const ansi = require('ansi-escape-sequences') const ansi = require('ansi-escape-sequences')
const cliOptions = require('../lib/cli-options') const cliOptions = require('../lib/cli-options')
@ -34,23 +33,11 @@ options.cli.server = Object.assign(options.builtIn, options.stored, options.cli.
if (options.cli.misc.help) return console.log(usage) if (options.cli.misc.help) return console.log(usage)
if (options.cli.misc.config) return console.log(JSON.stringify(options.stored, null, ' ')) if (options.cli.misc.config) return console.log(JSON.stringify(options.stored, null, ' '))
let log = {
format: options.cli.server['log-format']
}
if (log.format === 'none'){
log.format = undefined
} else if (log.format){
log.stream = process.stdout
} else {
log.format = 'common'
log.stream = streamLogStats({ refreshRate: 100 })
}
localWebServer({ localWebServer({
static: { root: options.cli.server.directory }, static: { root: options.cli.server.directory },
serveIndex: { path: options.cli.server.directory, options: { icons: true } }, serveIndex: { path: options.cli.server.directory, options: { icons: true } },
logger: { format: log.format, options: { stream: log.stream } }
logger: { format: options.cli.server['log-format'] },
compress: options.cli.server.compress
}).listen(options.cli.server.port, serverUp) }).listen(options.cli.server.port, serverUp)
function halt (message) { function halt (message) {
@ -64,6 +51,6 @@ function serverUp () {
if (path.resolve(options.cli.server.directory) === process.cwd()) { if (path.resolve(options.cli.server.directory) === process.cwd()) {
console.error(ansi.format(`serving at [underline]{http://localhost:${options.cli.server.port}}`)) console.error(ansi.format(`serving at [underline]{http://localhost:${options.cli.server.port}}`))
} else { } else {
console.error(ansi.format(`serving [underline]{options.cli.server.directory} at [underline]{http://localhost:${options.cli.server.port}}`))
console.error(ansi.format(`serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}`))
} }
} }

34
lib/local-web-server.js

@ -4,6 +4,8 @@ const serve = require('koa-static')
const convert = require('koa-convert') const convert = require('koa-convert')
const serveIndex = require('koa-serve-index') const serveIndex = require('koa-serve-index')
const morgan = require('koa-morgan') const morgan = require('koa-morgan')
const compress = require('koa-compress')
const streamLogStats = require('stream-log-stats')
/** /**
* @module local-web-server * @module local-web-server
@ -18,19 +20,47 @@ function getApp (options) {
options = Object.assign({ options = Object.assign({
static: {}, static: {},
serveIndex: {}, serveIndex: {},
logger: {}
logger: {},
compress: false
}, options) }, options)
const app = new Koa() const app = new Koa()
if (options.compress) {
console.log('comp');
app.use(convert(compress()))
}
let log = { format: options.logger.format }
if (options.logger.format) { if (options.logger.format) {
app.use(convert(morgan.middleware(options.logger.format, options.logger.options)))
if (log.format === 'none'){
log.format = undefined
} else if (log.format === 'logstalgia') {
/* customised logger :date token, purely to satisfy Logstalgia. */
morgan.token('date', function () {
var d = new Date()
return (d.getDate() + '/' + d.getUTCMonth() + '/' + d.getFullYear() + ':' + d.toTimeString())
.replace('GMT', '').replace(' (BST)', '')
})
log.format = 'combined'
} else if (log.format) {
log.stream = process.stdout
} }
} else {
log.format = 'common'
log.stream = streamLogStats({ refreshRate: 100 })
}
options.logger.options = options.logger.options || {}
options.logger.options.stream = log.stream
if (log.format) app.use(convert(morgan.middleware(log.format, options.logger.options)))
if (options.static.root) { 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) { 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

@ -27,6 +27,7 @@
"kcors": "^1.0.1", "kcors": "^1.0.1",
"koa": "^2.0.0-alpha.3", "koa": "^2.0.0-alpha.3",
"koa-charset": "^1.1.4", "koa-charset": "^1.1.4",
"koa-compress": "^1.0.8",
"koa-convert": "^1.1.0", "koa-convert": "^1.1.0",
"koa-json": "^1.1.1", "koa-json": "^1.1.1",
"koa-morgan": "^0.4.0", "koa-morgan": "^0.4.0",

13
test/test.js

@ -66,3 +66,16 @@ test('log: common', function (t) {
request('http://localhost:8100/') request('http://localhost:8100/')
.then(() => server.close()) .then(() => server.close())
}) })
test('compress', function(t){
const app = localWebServer({ compress: true })
})
function launchServer (app, onSuccess) {
const server = http.createServer(app.callback())
server.listen(8100)
const req = request('http://localhost:8100/')
if (onSuccess) req.then(onSuccess)
req.then(() => server.close())
}
Loading…
Cancel
Save