-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (51 loc) · 1.58 KB
/
server.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
const Vue = require('vue')
const express = require('express')
const fs = require('fs')
const { createBundleRenderer } = require('vue-server-renderer')
const setupDevServer = require('./build/setupDevServer')
const server = express()
server.use('/dist', express.static('./dist'))
const isProd = process.env.NODE_ENV === 'production'
let renderer
let onReady
if (isProd) {
const serverBundle = require('./dist/vue-ssr-server-bundle.json')
const clientManifest = require('./dist/vue-ssr-client-manifest.json')
const template = fs.readFileSync('./index.html', 'utf-8')
renderer = createBundleRenderer(serverBundle, {
template,
clientManifest
})
} else {
// 开发模式 -> 监视打包构建 -> 重新生成 Renderer 渲染期
onReady = setupDevServer(server, (serverBundle, clientManifest, template) => {
renderer = createBundleRenderer(serverBundle, {
template,
clientManifest
})
})
}
const render = async (req, res) => {
try {
const html = await renderer.renderToString({
title: 'my-vue-ssr',
meta: `
<meta name="description" content="my-vue-ssr" />
`,
url: req.url
})
res.setHeader('Content-Type', 'text/html; charset=utf8')
res.end(html)
} catch (err) {
res.status(500).end('Internal Server Error')
}
}
// 服务端路由设置为 *,意味着所有的路由都会进入这里
server.get('*', isProd ? render : async (req, res) => {
// 等待有了 Renderer 渲染期以后进行渲染
await onReady
render(req, res)
} )
server.listen(3000, () => {
console.log('Server is running at prot 3000')
})