如何使用react-router 2来设置服务器端渲染
我想移植我的应用程序,从1.0反应路由器2.0。 我正在尝试做快递服务器端渲染。
server.js代码(1.0)(工作)
var routes = require('./public/js/app.node.js'); app.get('*', function(req, res, next) { var location = new Location(req.path, req.query); try { Router.run(routes(), location, function(e, i, t) { var str = React.renderToString( React.createElement(Router, i)); }); } catch(e) { return next(); } });
server.js代码(2.0)(不工作)
var routes = require('./public/js/app.node.js'); app.get('*', function(req, res, next) { var location = new Location(req.path, req.query); try { match({routes, location: req.url} , function (error, redirectLocation, renderProps) { if (error) { res.status(500).send(error.message); } else if (redirectLocation) { res.redirect(302, redirectLocation.pathname + redirectLocation.search); } else if (renderProps) { var str = renderToString(React.createElement(RoutingContext, renderProps)); res.status(200).send(str); } else { res.status(404).send('Not found'); } }); }
routes.jsx:
export default (withHistory, onUpdate) => { return ( <Provider store={store}> <Router history={history} onUpdate={onUpdate}> <Route path='/' component={BarcodeListing} /> </Router> </Provider> ); };
上面的代码不匹配/
。 这是为什么。 console.log(req.url)
给我/
,然后/
在路线应该匹配/
在req.url权利!