Browse Source

cache-control extension example

master
Lloyd Brookes 9 years ago
parent
commit
6a2bbafd08
  1. 7
      bin/cli.js
  2. 17
      extend/cache-control.js
  3. 7
      lib/local-web-server.js
  4. 2
      package.json

7
bin/cli.js

@ -13,7 +13,6 @@ const t = require('typical')
const flatten = require('reduce-flatten')
const usage = commandLineUsage(cliOptions.usageData)
const stored = loadConfig('local-web-server')
let options
let isHttps = false
@ -36,16 +35,14 @@ if (options.misc.help) {
return
}
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.on('error', err => {
if (options.server['log-format']) {
console.error(ansi.format(err.message, 'red'))
}
})
const ws = localWebServer({
static: {
@ -64,7 +61,6 @@ if (options.misc.help) {
log: {
format: options.server['log-format']
},
cacheControl: options.server.cacheControl,
compress: options.server.compress,
mime: options.server.mime,
forbid: options.server.forbid,
@ -122,6 +118,7 @@ function onServerUp () {
}
function collectOptions () {
const stored = loadConfig('local-web-server')
let options = {}
/* parse command line args */

17
extend/cache-control.js

@ -0,0 +1,17 @@
'use strict'
const Koa = require('koa')
const localWebServer = require('../')
const cacheControl = require('koa-cache-control')
const convert = require('koa-convert')
const app = new Koa()
const ws = localWebServer({
'no-cache': true,
log: { format: 'dev' }
})
app.use(convert(cacheControl({
maxAge: 15
})))
app.use(ws)
app.listen(8000)

7
lib/local-web-server.js

@ -117,13 +117,6 @@ function localWebServer (options) {
middlewareStack.push(etag())
}
/* cache-control headers */
if (options.cacheControl) {
const cacheControl = require('koa-cache-control')
debug('Cache control', JSON.stringify(options.cacheControl))
app.use(cacheControl(options.cacheControl))
}
/* mime-type overrides */
if (options.mime) {
debug('mime override', JSON.stringify(options.mime))

2
package.json

@ -40,7 +40,6 @@
"koa": "^2.0.0",
"koa-bodyparser": "^3.0.0",
"koa-compose": "^3.1.0",
"koa-cache-control": "^1.0.0",
"koa-compress": "^1.0.9",
"koa-conditional-get": "^1.0.3",
"koa-connect-history-api-fallback": "~0.3.0",
@ -62,6 +61,7 @@
},
"devDependencies": {
"jsdoc-to-markdown": "^1.3.6",
"koa-cache-control": "^1.0.0",
"req-then": "~0.2.4",
"tape": "^4.5.1"
}

Loading…
Cancel
Save