-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (97 loc) · 3.25 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const path = require('path');
const fs = require('fs');
const express = require('express');
const LRU = require('lru-cache');
const serialize = require('serialize-javascript');
const bodyParser = require('body-parser');
const port = require('./config/sys.config').port;
const createBundleRenderer = require('vue-server-renderer').createBundleRenderer;
let indexHtml, // generated by html-webpack-plugin
renderer; // created from the webpack-generated server bundle
function createRenderer(serverBundle) {
return createBundleRenderer(serverBundle, {
cache: LRU({
max: 1000,
maxAge: 1000 * 60 * 15
})
})
}
function parseIndex(template) {
const contentMarker = '<!--APP-->';
const i = template.indexOf(contentMarker);
return {
head: template.slice(0, i),
tail: template.slice(i + contentMarker.length)
}
}
const app = express();
let server;
if (process.argv.includes('--development') && process.argv.includes('--https')) {
server = require('https').createServer({
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem')
}, app);
} else {
server = require('http').createServer(app);
}
// 初始化socket,及注册相关事件
require('./init-socket')(server);
app.use(express.static('public'));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: false
}))
if (process.argv.indexOf('--development') > -1) {
require('./build/setup-dev-server')(app, {
bundleUpdated: serverBundle => {
renderer = createRenderer(serverBundle);
},
indexUpdated: indexTpl => {
indexHtml = parseIndex(indexTpl);
}
})
} else {
const bundlePath = path.resolve(process.cwd(), './dist/server/server-bundle.js');
const indexTplPath = path.resolve(process.cwd(), './dist/static/index.html');
renderer = createRenderer(fs.readFileSync(bundlePath, 'utf-8'));
indexHtml = parseIndex(fs.readFileSync(indexTplPath, 'utf-8'));
app.use('/static', express.static(path.resolve(process.cwd(), 'dist/static')));
}
app.get('*', (req, res) => {
if(!renderer) {
return res.end('waiting for compilation... refresh in a moment.');
}
const context = {
path: req.path
}
const renderStream = renderer.renderToStream(context);
res.setHeader('Content-Type', 'text/html');
res.write(indexHtml.head);
renderStream.on('data', chunk => {
res.write(chunk);
});
renderStream.on('end', () => {
res.write(
`<script>
window.__INITIAL_STATE__= ${serialize(context.initialState, {isJSON: true})}
</script>`
);
res.end(indexHtml.tail);
});
renderStream.on('error', err => {
if (err && err.code == '404') {
res.status(404).end('404 | Page Not Found');
return ;
}
res.status(500).end('Internal Error 500');
console.error(`error during render: ${req.path}`);
console.error(err);
})
});
app.post('/test', (req, res) => {
console.log('hash========>', req.body.hash);
res.send('ok');
})
server.listen(port, () => {
console.log(`==> Listening at ${process.argv.includes('--https') ? 'https' : 'http'}://localhost:${port}`)
})