简单的服务器/客户端路由react-express(nodejs)

时间:2017-09-15 18:54:55

标签: node.js reactjs render-to-string react-dom-server

App.js有一个链接 - 我不知道我是否需要动作创作者/减少者?如果是这样如何与服务器端渲染连接?

基本上在加载otherComponent时 - 应该通过path从server.js进行API调用并将响应注入组件 - 这个图片中的响应动作在哪里?

server.js:

app.get('/test', (req, res) => {
    res.send(<otherComponent />); -> this is returning json right now? How to handle this path for example here - where this should make an api call.
});

app.get('*', (req, res) => {
  res.send(`
            <!doctype html>
            <html>
                <head>
                    <title>My website</title>
                    <meta name="viewport" content="width=device-width, initial-scale=1">
            </head>
                <body>
                    <div id='app'>${renderToString(
                        <Provider store={createStore(reducers)}>
                            <StaticRouter location={req.url} context={{}}>
                                <App />
                            </StaticRouter>
                        </Provider>
                    )}</div>
                    <script src='bundle.js'></script>

                </body>
            </html>
        `);
});

3 个答案:

答案 0 :(得分:0)

在您的React组件中,只需触发您需要的API调用

componentDidMount() {
    $.get('/test', data => {
        // use data
    });
} 

通常使用SPA,您可以在第一次请求时从服务器提供完整的客户端应用程序,对您的视图使用客户端路由,然后触发任何API请求(如示例)。

答案 1 :(得分:0)

<强>更新 我想您可能会发现本教程很有用:https://medium.com/@foxhound87/server-side-rendering-with-react-router-we-must-react-ep-04-ad03b6b9e05d

使用 create-react-app 来创建您的应用。 yarn add react-router-dom添加React的路由器。有关详细信息,请访问:https://reacttraining.com/react-router/web/guides/quick-start

  1. <Router />放在App JSX周围。
  2. 使用<Link />创建指向<Route />的链接。当路径 在链接匹配中,正确的路线渲染。
  3. 还请注意componentDidMount
  4. 中的请求
    import React, { Component } from 'react';
    import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
    import logo from './logo.svg';
    import './App.css';
    
    class App extends Component {
      render() {
        return (
          <Router>
            <div className="App">
              <div className="App-header">
                <img src={logo} className="App-logo" alt="logo" />
                <h2>Welcome to React</h2>
              </div>
              <nav>
                <Link to="/">Home</Link>
                <Link to="/test">Test</Link>
              </nav>
              <Route path="/" exact component={Index} />
              <Route path="/test" exact component={Test} />
            </div>
          </Router>
        );
      }
    }
    
    class Index extends Component {
      onComponentDidMount() {
        fetch('http://yourapi.com')
          .then(data => this.setState(data));
      }
      render() {
        return (
          <div>
            <h1>Index Component</h1>
            <div>{ /* do something with this.state.data */}</div>
          </div>
        );
      }
    }
    
    class Test extends Component {
      onComponentDidMount() {
        fetch('http://yourapi.com')
          .then(data => this.setState(data));
      }
      render() {
        return (
          <div>
            <h1>Test Component</h1>
            <div>{ /* do something with this.state.data */}</div>
          </div>
        );
      }
    }
    
    export default App;
    

答案 2 :(得分:0)

<otherComponent \>的定义中,获取componentDidMount()中的数据,然后执行store.dispatch({ type: 'RECEIVED_DATA', data })更新商店。