-
Notifications
You must be signed in to change notification settings - Fork 234
/
server.js
85 lines (78 loc) · 3.12 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
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
/**
* Created by kenkozheng on 2017/11/27.
*/
const fs = require('fs');
const path = require('path');
const LRU = require('lru-cache');
const express = require('express');
const server = express();
const { createBundleRenderer } = require('vue-server-renderer');
const isProd = process.env.NODE_ENV === 'production';
const resolve = file => path.resolve(__dirname, file);
const serve = (path, cache) => express.static(resolve(path), {
maxAge: cache && isProd ? 1000 * 60 * 60 * 24 * 30 : 0
});
const createRenderer = (bundle, options) => createBundleRenderer(bundle, Object.assign(options, {
// for component caching
cache: LRU({
max: 1000,
maxAge: 1000 * 60 * 15
}),
// recommended for performance
runInNewContext: false
}));
let render;
const templatePath = resolve('../web/tpl.html');
let baseRender = (renderer, req, res) => {
//context是一个对象,在模版中,使用<title>{{ title }}</title>方式填充 https://ssr.vuejs.org/zh/basic.html
let context = {title: 'VueSSR Multipages'};
renderer.renderToString(context, (err, html) => {
if (err) {
console.log(err);
res.status(500).end('Internal Server Error');
return
}
res.send(html);
res.end();
});
};
if (isProd) {
// In production: create server renderer using template and built server bundle.
// The server bundle is generated by vue-ssr-webpack-plugin.
const template = fs.readFileSync(templatePath, 'utf-8')
const bundle = require('../dist/vue-ssr-server-bundle.json') //todo 多页面需要有个列表文件列出所有bundle,然后这里建立多个renderer
// The client manifests are optional, but it allows the renderer
// to automatically infer preload/prefetch links and directly add <script>
// tags for any async chunks used during render, avoiding waterfall requests.
const clientManifest = require('../dist/vue-ssr-client-manifest.json')
let renderer = createRenderer(bundle, {
template,
clientManifest
});
//多页面,todo 根据请求选对应的bundle renderer处理
render = (req, res) => {
baseRender(renderer, req, res);
};
} else {
// In development: setup the dev server with watch and hot-reload,
// and create a new renderer on bundle / index template update.
// devserver使用的是webpack-dev-middleware,过程文件存储在内存
const devServerSetup = require('../build/setup-dev-server');
let renderer;
var promise = devServerSetup(server, templatePath, (bundle, options) => {
renderer = createRenderer(bundle, options); //刷新renderer
});
render = (req, res) => {
promise.then(() => baseRender(renderer, req, res)); //需要等待文件初始化
};
}
server.use('/dist', serve('../dist')); //静态目录
server.use('/public', serve('../web/public'));
/**
* 不建议在server.js中写太多路由的事情,如果路由多了,建议迁移到额外一个配置表中
*/
server.get('*', render);
const port = 80;
server.listen(port, () => {
console.log(`server started at localhost:${port}`)
});