-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (43 loc) · 1.28 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
const express = require('express');
const ejs = require('ejs');
const path = require('path');
const fs = require('fs');
const { createBundleRenderer } = require('vue-server-renderer')
//创建渲染器
let renderer = createBundleRenderer(
path.join(process.cwd(), './bundle/vue-ssr-server-bundle.json'),
{
template: fs.readFileSync(path.join(process.cwd(), './views/index.html'), 'utf-8')
}
)
let app = express();
app.engine('.html', ejs.__express);
app.use('/static/', express.static(path.join(__dirname, './static/')))
app.get('*', (req, res) => {
// res.render('index.html');
renderer.renderToString({
// 传递路由url
url: req.url,
// 插值注入
title: 'vue-ssr',
meta: `
<meta name="keywords" content="vue webpack ssr">
<meta name="description" content="vue-ssr">
`,
// 同步数据
data: {
msg: 'hello vue-ssr',
description: '前后端同步渲染',
}
})
.then(html => res.end(html))
.catch(err => {
if(err.code === 404) {
res.writeHead(404, {'Content-Type': 'text/plain; utf-8'}).end(err.msg);
}
else {
res.status(500).end('服务器错误');
}
})
})
app.listen(3005);