-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.js
69 lines (61 loc) · 1.63 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
const fs = require('fs')
const path = require('path')
const express = require('express')
const renderer = require('vue-server-renderer').createRenderer()
const app = express()
// Server-Side Bundle File
const createApp = require('./dist/bundle.server.js')['default']
// Client-Side Bundle File
const clientBundleFileUrl = '/bundle.client.js'
app.use('/', express.static(__dirname + '/dist'))
// api
app.get('/api/getList', (req, res) => {
res.json(
{
list: [
{'title': 11},
{'title': 22},
{'title': 33},
{'title': 44}
]
}
)
})
// Server-Side Rendering
app.get('*', (req, res) => {
const context = { url: req.url }
createApp(context).then(app => {
renderer.renderToString(app, (err, html) => {
if (err){
res.status(500).send(`
<h1>Error: ${err.message}</h1>
<pre>${err.stack}</pre>
`)
} else {
res.send(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 2.0 SSR</title>
</head>
<body>
<div id="app">
${html}
</div>
<script>window.__INITIAL_STATE__ = ${JSON.stringify(context.state)}</script>
<script src="${clientBundleFileUrl}"></script>
</body>
</html>`)
}
});
}, err => {
if (err.code === 404) {
res.status(404).end('Page not found')
} else {
res.status(500).end('Internal Error')
}
})
})
// Start server
app.listen(8080)