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 Koa = require('koa') const app = new Koa() const fs = require('fs') const path = require('path') const { createBundleRenderer } = require('vue-server-renderer')
const resolve = file => path.resolve(__dirname, file)
// 生成服务端渲染函数 const renderer = createBundleRenderer(require('./dist/vue-ssr-server-bundle.json'), { // 推荐 runInNewContext: false, // 模板html文件 template: fs.readFileSync(resolve('./index.html'), 'utf-8'), // client manifest clientManifest: require('./dist/vue-ssr-client-manifest.json') })
function renderToString (context) { return new Promise((resolve, reject) => { renderer.renderToString(context, (err, html) => { err ? reject(err) : resolve(html) }) }) } app.use(require('koa-static')(resolve('./dist'))) // response app.use(async (ctx, next) => { try { const context = { title: '服务端渲染测试', // {{title}} url: ctx.url } // 将服务器端渲染好的html返回给客户端 ctx.body = await renderToString(context)
// 设置请求头 ctx.set('Content-Type', 'text/html') ctx.set('Server', 'Koa2 server side render') } catch (e) { // 如果没找到,放过请求,继续运行后面的中间件 next() } })
app.listen(3001)
|