forked from 2662419405/AllDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (34 loc) · 1 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
const fs = require("fs");
const express = require('express')
const app =express()
// 开放dist目录
app.use(express.static('./dist'))
// 第 2 步:获得一个createBundleRenderer
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 renderer = createBundleRenderer(bundle, {
runInNewContext: false,
template: fs.readFileSync("./src/index.temp.html", "utf-8"),
clientManifest: clientManifest
});
function renderToString(context) {
return new Promise((resolve, reject) => {
renderer.renderToString(context, (err, html) => {
resolve(html);
});
});
}
app.get('*',async (req,res)=>{
console.log(req.url,123)
const context = {
title:'ssr test',
url:req.url
}
const html = await renderToString(context);
res.send(html)
})
const port = 3001;
app.listen(port, function() {
console.log(`server started at localhost:${port}`);
});