-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
36 lines (32 loc) · 1.14 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
const express = require('express');
const server = express();
const path = require('path');
const { createBundleRenderer } = require('vue-server-renderer');
const serverBundle = require('./.bundle/vue-ssr-server-bundle.json');
const clientManifest = require('./dist/vue-ssr-client-manifest.json');
const renderer = createBundleRenderer(serverBundle, {
runInNewContext: false,
template: require('fs').readFileSync('./src/index.template.html', 'utf-8'),
clientManifest
})
server.use('/js', express.static(path.join(__dirname, './dist/js')));
server.use('/css', express.static(path.join(__dirname, './dist/css')));
server.use(express.static('static'));
server.get('*', (req, res) => {
const ctx = {
url: req.url,
title: 'Vue SSR Example',
meta: `<meta data-n-head content="SSR Page" name="application-name">`
};
renderer.renderToString(ctx, (err, html) => {
if(err) {
res.status(500).end('Internal Server Error');
return;
};
res.status(200);
res.end(html)
})
});
server.listen(8080, function() {
console.log("Listening app at http://localhost:8080");
});