-
Notifications
You must be signed in to change notification settings - Fork 83
/
server.js
46 lines (41 loc) · 1.41 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
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");
const serialize = require('serialize-javascript');
const { createBundleRenderer } = require('vue-server-renderer');
const isProd = typeof process.env.NODE_ENV !== 'undefined' && (process.env.NODE_ENV === 'production')
let renderer;
const indexHTML = (() => {
return fs.readFileSync(path.resolve(__dirname, "./index.html"), "utf-8");
})();
if (isProd) {
app.use("/", express.static(path.resolve(__dirname, "./dist")));
} else {
app.use("/dist", express.static(path.resolve(__dirname, "./dist")));
}
if (isProd) {
const bundlePath = path.resolve(__dirname, './dist/server/main.js')
renderer = createBundleRenderer(fs.readFileSync(bundlePath, 'utf-8'))
} else {
require("./build/dev-server")(app, bundle => {
renderer = createBundleRenderer(bundle)
});
}
app.get("*", (req, res) => {
const context = { url: req.url };
renderer.renderToString(context, (err, html) => {
if (err) {
return res.status(500).send('Server Error')
}
html = indexHTML.replace('{{ APP }}', html)
html = html.replace('{{ STATE }}',
`<script>window.__INITIAL_STATE__=${serialize(context.initialState, { isJSON: true })}</script>`)
res.write(html);
res.end();
})
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`server started at http://localhost:${port}`);
});