-
Notifications
You must be signed in to change notification settings - Fork 6
/
koa.js
64 lines (44 loc) · 1.09 KB
/
koa.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
'use strict'
/**
* Module dependencies
*/
const Koa = require('koa')
const fs = require('fs')
const path = require('path')
const Router = require('koa-router')
const logger = require('koa-logger')
/**
* To test, publish something on 'test-koa' or 'test-koa1' channel
*
* $ redis-cli publish test-koa testmessage
* $ redis-cli publish test-koa1 testmessage1
*/
// stream
const subscribe = require('..')
// app
let app = new Koa()
// logger
app.use(logger())
// routes
let router = new Router()
router.get('/', function *() {
this.type = 'text/html; charset=utf-8'
this.body = fs.createReadStream(__dirname + '/index.html')
})
router.get('/stream', function *() {
this.req.setTimeout(Number.MAX_VALUE)
this.type = 'text/event-stream; charset=utf-8'
this.set('Cache-Control', 'no-cache')
this.set('Connection', 'keep-alive')
this.body = subscribe({
channels: ['test-koa', 'test-koa1'],
retry: 10000,
host: '127.0.0.1',
port: 6379,
channelsAsEvents: true
})
})
app.use(router.routes())
app.listen(3000, () => {
console.log('Koa listening on port 3000')
})