-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
142 lines (123 loc) · 3.66 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
process.env.VUE_ENV = 'server'
const isProd = process.env.NODE_ENV === 'production'
const fs = require('fs')
const path = require('path')
const express = require('express')
const favicon = require('serve-favicon')
const compression = require('compression')
const serialize = require('serialize-javascript')
const resolve = file => path.resolve(__dirname, file)
const app = express()
let indexHTML // generated by html-webpack-plugin
let joseHTML // spa页面
let phineHTML // spa页面
let renderer // created from the webpack-generated server bundle
if (isProd) {
// in production: create server renderer and index HTML from real fs
renderer = createRenderer(fs.readFileSync(resolve('./dist/server/server-bundle.js'), 'utf-8'))
indexHTML = parseIndex(fs.readFileSync(resolve('./dist/app.html'), 'utf-8'))
} else {
// in development: setup the dev server with watch and hot-reload,
// and update renderer / index HTML on file change.
require('./build/setup-dev-server')(app, {
bundleUpdated: bundle => {
renderer = createRenderer(bundle)
},
indexUpdated: index => {
indexHTML = parseIndex(index)
},
joseUpdated: index => {
joseHTML = index
},
phineUpdated: index => {
phineHTML = index
}
})
}
function createRenderer(bundle) {
// https://github.com/vuejs/vue/blob/next/packages/vue-server-renderer/README.md#why-use-bundlerenderer
return require('vue-server-renderer').createBundleRenderer(bundle, {
cache: require('lru-cache')({
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 serve = (path, cache) => express.static(resolve(path), {
maxAge: cache && isProd ? 60 * 60 * 24 * 30 : 0
})
app.use(compression({ threshold: 0 }))
app.use(favicon('./src/assets/img/logo-48.png'))
app.use('/manifest.json', serve('./manifest.json'))
app.use('/server', serve('./dist/server'))
app.use('/static', serve('./dist/static'))
app.get(['/', '/topic','/home'], (req, res) => {
if (!renderer) {
return res.end('waiting for compilation... refresh in a moment.')
}
res.setHeader("Content-Type", "text/html");
var s = Date.now()
const context = { url: req.url }
const renderStream = renderer.renderToStream(context)
renderStream.once('data', () => {
res.write(indexHTML.head)
})
renderStream.on('data', chunk => {
res.write(chunk)
})
renderStream.on('end', () => {
// embed initial store state
if (context.initialState) {
res.write(
`<script>window.__INITIAL_STATE__=${
serialize(context.initialState, { isJSON: true })
}</script>`
)
}
res.end(indexHTML.tail)
console.log(`whole request: ${Date.now() - s}ms`)
})
renderStream.on('error', err => {
if (err && err.code === '404') {
res.status(404).end('404 | Page Not Found')
return
}
// Render Error Page or Redirect
res.status(500).end('Internal Error 500')
console.error(`error during render : ${req.url}`)
console.error(err)
})
})
//spa页面'
app.get(['/counter', '/poem'], (req, res) => {
if (isProd) {
res.render('jose.html')
} else {
res.send(joseHTML)
}
})
//spa页面
app.get(['/model', '/show'], (req, res) => {
if (isProd) {
res.render('phine.html')
} else {
res.send(phineHTML)
}
})
//其他路由404
app.get('*', (req, res) => {
//res.render('404.html')
res.send('HTTP STATUS: 404')
})
const port = process.env.PORT || 8080
app.listen(port, () => {
console.log(`server started at http://localhost:${port}`)
})