-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
84 lines (72 loc) · 2.38 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// You will need redis to be installed and running on default settings to run
// this example without errors
const debug = require('debug')('jsw:index')
const koa = require('koa')
const hb = require('koa-handlebars')
const static = require('koa-static')
const router = require('koa-router')()
const redis = require('redis')
const config = require('config3')
const path = require('path')
const app = koa()
const db = redis.createClient()
// getValue - a function demonstrating how to "promisify" a traditional callback
// based function. This is used to get values from redis using the GET command,
// and returns a promise that you can yield safely.
const getValue = function (key) {
return new Promise((resolve, reject) => {
db.get(key, (err, reply) => {
if (err) {
return reject(err)
}
resolve(reply.toString())
})
})
}
// Performance tracking. This is a simple example of how to measure a request's
// performance using the yield keyword in a koa middleware.
app.use(function *(next) {
const start = Date.now()
yield next
const end = Date.now()
debug(`${this.request.method} ${this.request.url}\t${end - start}ms`)
})
// Template support. We set views and layouts to the same directory to simplify
// matters. In production this should be set to separate directories to promote
// better separation of concerns.
app.use(hb({
defaultLayout: 'main',
root: __dirname,
viewsDir: 'view',
layoutsDir: 'view'
}))
// Sets a value into redis using the SET command, and renders a view after that
router.get('/set/:key/:value', function *() {
const key = this.params.key
const value = this.params.value
db.set(key, value)
yield this.render('talk', {
msg: `set ${key} to ${value}`
})
})
// Gets the value of a key in redis using the GET command and renders a view
// showing that value
router.get('/get/:key', function *() {
const key = this.params.key
const value = yield getValue(key)
yield this.render('talk', {
msg: value
})
})
// Attach the router to the koa app as a middleware
app.use(router.routes())
.use(router.allowedMethods())
// Make sure this is dead last. Serves static files from public
app.use(static(path.join('.', 'public')))
// Only start the app if this is run directly by node (so we can easily test)
if (require.main === module) {
debug(`Listening to port ${config.port}`)
app.listen(config.port)
} else {
module.exports = app
}