-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (38 loc) · 1.16 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
const Koa = require('koa');
const app = new Koa();
const path = require('path')
const Router = require('koa-router');
const fs = require('fs');
const { renderToString } = require("@vue/server-renderer");
let port = 8080
let server = require('http').createServer(app.callback());
app.use(require('koa-static')(__dirname + '/dist'));
require('koa-qs')(app)
app.use(require('koa-bodyparser')());
let testDatas = [
{
title:"title 1"
,conctent:"conctent 1"
},
{
title:"title 2"
,conctent:"conctent 2"
}
]
let router = new Router();
router.get('/api', async function (ctx, next) {
ctx.body = testDatas
});
app.use(router.routes()).use(router.allowedMethods())
let viewPath = path.join(__dirname, "dist/index.html");
let appPath = path.join(__dirname, "distssr/js/server");
const createSSRApp = require(appPath).default;
app.use(async ctx => {
const app = await createSSRApp(ctx);
const appContent = await renderToString(app);
let temp = fs.readFileSync(viewPath, {encoding: 'utf-8'});
ctx.body = temp.replace('{{appContent}}',appContent)
});
server.listen(port, function () {
console.log("start " + port)
});