服务器端渲染无法自动更新

来源:2-8 开发时的服务端渲染

Joeeeee

2017-12-08

```

const axios = require('axios');

const webpack = require('webpack');

const path = require('path');

const MemoryFs = require('memory-fs');

const proxy = require('http-proxy-middleware');

const ReactDomServer = require('react-dom/server');


const serverConfig = require('../../build/webpack.config.server');


const getTemplate = () => {

return new Promise((resolve, reject) => {

axios.get('http://localhost:8888/public/index.html')

.then((res) => {

resolve(res.data);

})

.catch(reject);

});

};


const Module = module.constructor;


const mfs = new MemoryFs();

const serverCompiler = webpack(serverConfig);

serverCompiler.outputFileSystem = mfs;

let serverBundle;

serverCompiler.watch({}, (err, stats) => {

if (err) throw err;

stats = stats.toJson();

stats.errors.forEach(err => console.error(err));

stats.warnings.forEach(warn => console.warn(warn));


const bundlePath = path.join(

serverConfig.output.path,

serverConfig.output.filename,

);

const bundle = mfs.readFileSync(bundlePath, 'utf-8');

const m = new Module();

m._compile(bundle, 'server-entry.js');

serverBundle = m.exports.default;

});


module.exports = function(app) {


app.use('/public', proxy({

target: 'http://localhost:8888',

}));


app.get('*', (req, res) => {

getTemplate().then((template) => {

const content = ReactDomServer.renderToString(serverBundle);

res.send(template.replace('<!-- app -->', content));

});

});

};


```

照着视频来敲的,哪里出了问题?


热更新返回的json是html来的

写回答

1回答

Jokcy

2017-12-08

你看一下webpack配置的output.publicPath,确保他的值是'/public/'而不是'/public'。

0
6
慕桂英5446361
回复
Joeeeee
回复 吴小粥怎么解决的 在哪里的问题 我的没有解决
2019-04-08
共6条回复

React全栈+服务器渲染(ssr)打造社区Webapp

【毕设面试】只会写业务代码?out了,带你学会搭建属于自己的工程!

768 学习 · 414 问题

查看课程