koa-compress

Compress middleware for Koa

Example

1
2
3
4
5
6
7
8
9
const compress = require('koa-compress')
const Koa = require('koa')

const app = new Koa()
app.use(compress({
filter: (content_type) => /text/i.test(content_type),
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH,
}))

Options

The options are passed to zlib

filter: An optional function that checks the response content type to decide whether to compress. By default, it uses compressible

threshold: Minimum response size in bytes to compress. Default 1024 byte or 1kb.

koa-morgan

HTTP Request logger middleware for node.js

1
const morgan = require('morgan')

morgan(format, options)

Create a new morgan logger middleware function using the given format and options.

The format argument amay be a string of a predefined name, a string fo a format string, or a function that will produce a log entry.

The format function will be called with three arguments tokens, req and res, where tokens is object with all tokens, req is the HTTP request and res is the HTTP response. The function is expected to return a string that will be the log line, or undefined/null to skip logging.

predefined format string

combined: Standard Apache combined log output

1
:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"

common: Standard Apache common log output

dev

short

tiny

1
:method :url :status :res[content-length] - :response-time ms

write logs to a file

Single file

Simple app that will log all request in the Apache combined format to the file access.log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const fs = require('fs')
const Koa = require('koa')
const morgan = require('koa-morgan')

const accessLogStream = fs.createWriteStream(__dirname + '/access.log', { flags: 'a' })

const app = new Koa()

// setup the logger

app.use(morgan('combined', { stream: accessLogStream }))

app.use((ctx) => {
ctx.body = 'hello world'
})

app.listen(3000)

koa-session

Simple session middleware for koa. Default is cookie-based session and support external store.

Require Node 7.6 or greater for asycn/await support

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const session = require('koa-session')
const Koa = require('koa')
const app = new Koa()

app.keys = ['some secret hurr']

const CONFIG = {
key: 'koa:sess', // (string) cookie key, default is koa:sess
maxAge: 86400000, // (number) in ms, default is 1 day,
overWrite: true, // (boolean) can overwrite or not, default true
httpOnly: true, // (boolean) httpOnly or not, default true
signed: true, // (boolean) signed or not, default true
rolling: false, // (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge.
}

app.use(session(CONFIG, app)) // or if you prefer to default config, you can use => app.use(session(app))

app.use(ctx => {
if (ctx.path === '/favicon.ico') return
let n = ctx.session.views || 0
ctx.session.views = ++n
ctx.body = n + ' views'
})

app.listen(3000)
console.log('listening on port 3000')

Options

The cookie name is controlled by the key option, which default to ‘koa:sess’. All other options are passed to ctx.cookies.get() and ctx.cookies.set() allowing you to control security, domain, path and signing among other setting.