-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (107 loc) · 3.02 KB
/
app.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const Koa = require('koa')
const Router = require('koa-router')
const render = require('koa-swig')
const co = require('co')
const path = require('path')
const fs = require('fs')
const cheerio = require('cheerio')
const { Readable } = require('stream')
const app = new Koa()
const router = new Router()
const task1 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
// resolve('我是第一次输出<br/>')
resolve(`<script>addHtml('part1', '我是第一次输出')</script>`)
}, 2000)
})
}
const task2 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
// resolve('我是第二次输出')
resolve(`<script>addHtml('part2', '我是第二次输出')</script>`)
}, 2000)
})
}
app.context.render = co.wrap(render({
root: path.join(__dirname, 'views'),
autoescape: true,
// ssr 最关键的地方
// cache: 'memory', // disable, set to false
cache: false,
ext: 'html',
writeBody: false,
}));
router.get('/', async (ctx, next) => {
ctx.status = 200
ctx.type = 'html'
const filename = path.resolve(__dirname, 'index.html')
const stream = fs.createReadStream(filename)
// demo1
// const file = fs.readFileSync('./index.html', 'utf-8')
// ctx.res.write(file)
// ctx.res.end()
// demo2 页面输出ok, 说明状态是好的,但是没有输出数据
// stream.pipe(ctx.res)
// demo3
// function createSsrStreamPromise () {
// return new Promise((resolve, reject) => {
// stream.on('error', err => {reject(err)}).pipe(ctx.res)
// })
// }
// await createSsrStreamPromise()
// demo4 报错 write after end
// stream.on('data', (chunk) => {
// ctx.res.write(chunk)
// })
// demo5
// function createSsrStreamPromise () {
// return new Promise((resolve, reject) => {
// const stream = fs.createReadStream(filename)
// stream.on('data', (chunk) => {
// ctx.res.write(chunk)
// })
// })
// }
// await createSsrStreamPromise();
// demo6 体验bigpipe
const file = fs.readFileSync('./index.html', 'utf-8')
ctx.res.write(file)
const result1 = await task1()
ctx.res.write(result1)
const result2 = await task2()
ctx.res.write(result2)
ctx.res.end()
})
router.get('/index', async (ctx, next) => {
ctx.body = await ctx.render('index')
})
router.get('/about', async (ctx, next) => {
console.log('about')
ctx.status = 200
ctx.type = 'html'
const result = await ctx.render('about')
if (ctx.request.header['x-pjax']) {
const $ = cheerio.load(result)
$('.pjaxcontent').each(function () {
ctx.res.write($(this).html())
})
ctx.res.end()
} else {
function createSsrStreamPromise () {
return new Promise((resolve, reject) => {
const stream = new Readable()
stream.push(result)
stream.push(null)
stream.on('error', err => {reject(err)}).pipe(ctx.res)
})
}
await createSsrStreamPromise()
}
})
app.use(router.routes())
.use(router.allowedMethods())
app.listen(8080, () => {
console.log('服务已经启动...🍺')
})