-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.js
53 lines (42 loc) · 1.36 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
'use strict';
const express = require('express');
const fs = require('fs');
const path = require('path');
const { createBundleRenderer } = require('vue-server-renderer');
const bundle = require('./dist/vue-ssr-server-bundle.json');
const clientManifest = require('./dist/vue-ssr-client-manifest.json');
const template = fs.readFileSync('./src/index.template.html', 'utf-8');
const PORT = 3000;
const server = express();
const renderer = createBundleRenderer(bundle, {
template: template,
clientManifest: clientManifest,
runInNewContext: false
});
server.use('/dist', express.static(path.resolve(__dirname, './dist')));
server.use('/public', express.static(path.resolve(__dirname, './public')));
server.use('/manifest.json', express.static(path.resolve(__dirname, './manifest.json')));
server.use('/service-worker.js', express.static(path.resolve(__dirname, './dist/service-worker.js')));
server.get('*', (req, res) => {
res.setHeader("Content-Type", "text/html");
const context = {
url: req.url,
title: "Headline News"
};
renderer.renderToString(context, (err, html) => {
if(err){
if(err.code === 404){
res.status(400).send('Not found');
}else{
console.log(err);
res.status(500).send('Internal server error');
}
}else{
res.send(html);
}
});
});
const port = PORT || 3000;
server.listen(port, () => {
console.log("Server started")
});