【已解决】ERROR: connect ECONNREFUSED 127.0.0.1:8000
来源:5-3 服务端渲染的entry配置
warzsy
2018-09-11
dev-ssr.js 代码我对比之后是跟老师完全一样的
:
const Router = require(‘koa-router’)
const axios = require(‘axios’)
const path = require(‘path’)
const fs = require(‘fs’)
const MemoryFS = require(‘memory-fs’)
const webpack = require(‘webpack’)
const VueServerRenderer = require(‘vue-server-renderer’)
const serverRender = require(’./server-render’)
const serverConfig = require(’…/…/build/webpack.config.server’)
const serverCompiler = webpack(serverConfig)
const mfs = new MemoryFS()
serverCompiler.outputFileSystem = mfs
let bundle
serverCompiler.watch({}, (err, stats) => {
if (err) throw err
stats = stats.toJson()
stats.errors.forEach(err => console.log(err))
stats.warnings.forEach(warn => console.warn(err))
const bundlePath = path.join(
serverConfig.output.path,
‘vue-ssr-server-bundle.json’
)
bundle = JSON.parse(mfs.readFileSync(bundlePath, ‘utf-8’))
console.log(‘new bundle generated’)
})
const handleSSR = async (ctx) => {
if (!bundle) {
ctx.body = '你等一会,别着急…'
return
}
const clientManifestResp = await axios.get(
‘http://127.0.0.1:8000/public/vue-ssr-client-manifest.json’
)
const clientManifest = clientManifestResp.data
const template = fs.readFileSync(
path.join(__dirname, ‘…/server.template.ejs’),
‘utf-8’
)
const renderer = VueServerRenderer
.createBundleRenderer(bundle, {
inject: false,
clientManifest
})
await serverRender(ctx, renderer, template)
}
const router = new Router()
router.get(’*’, handleSSR)
module.exports = router
1回答
-
warzsy
提问者
2018-09-11
已解决
先执行 cnpm run dev:client
00
相似问题