-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.js
52 lines (36 loc) · 975 Bytes
/
example.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const Koa = require('koa')
const crypto = require('mz/crypto')
const app = new Koa()
require('./')(app)
app.use(async function (ctx, next) {
const start = Date.now()
ctx.id = await crypto.randomBytes(12)
ctx.trace('start')
await next()
ctx.trace('finish')
ctx.set('X-Response-Time', (Date.now() - start) + 'ms')
})
app.use(async function (ctx, next) {
ctx.trace('wait:before')
function delay () {
return new Promise((resolve) => {
setTimeout(resolve, Math.random() * 100)
})
}
await delay()
ctx.trace('wait:after')
await next()
})
app.use(async function (ctx, next) {
ctx.trace('user.id', random())
ctx.trace('some.event', random(), random())
ctx.trace('another.event', random(), random())
ctx.body = 'hello world!'
})
app.debug()
const port = process.env.PORT || 3210
app.listen(port)
console.log('koa-trace example listening on port %s', port)
function random() {
return Math.random().toString(36).slice(2)
}