不推荐使用router.run转换为最新的代码

时间:2016-11-23 11:36:52

标签: node.js express reactjs redux

我从某个地方拿起了一个试图学习反应redux nodejs的样本,不幸的是我研究过并在现有代码中做了很多改动。后来我意识到我选择的代码使用的是旧版本的反应。我修复了所有问题,除了编写以下部分而不破坏server.jsx

中的功能
 app.use((req, res) => {
  delete process.env.BROWSER;
  const logger = store => next => action => {
     let result = next(action);
     return result;
  };

  const location = createLocation(req.url);
  const reducer = combineReducers(reducers);
  let createStoreWithMiddleware = applyMiddleware(promiseMiddleware, logger)(createStore);
  const store = createStoreWithMiddleware(reducer);

  Router.run(routes, location, (err, routeState) => {
    if (err) return console.error(err);
    if (!routeState) return res.status(404).end('404');
    const InitialComponent = (
      <Provider store={store}>
        {() =>
          <Router {...routeState} />
        }
      </Provider>
    );

    const componentHTML = React.renderToString(InitialComponent);
    const initialState = store.getState();
    const HTML = `
    <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          <title>BookMyDoc</title>
            <link rel="stylesheet" href="/css/bootstrap.min.css">
          <link rel="stylesheet" href="/css/react-select.css">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
          <script type="application/javascript">
            window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
          </script>
        </head>
        <body>
          <div id="react-view">${componentHTML}</div>
          <script type="application/javascript" src="/bundle.js"></script>
          <script src="/js/jquery.min.js"></script>
          <script src="/js/bootstrap.min.js"></script>
        </body>
      </html>
    `
    res.end(HTML);
  });
});

请帮我在更新版本中做同样的事情。

0 个答案:

没有答案