【问题标题】:What's wrong with this ReactRouter.match() implementation?这个 ReactRouter.match() 实现有什么问题?
【发布时间】:2017-08-05 11:17:57
【问题描述】:

我正在尝试通过 express 从我的服务器提供 React。

但是,当我点击 localhost:3000 时,我遇到的错误是:

TypeError: (0 , _reactRouter.match) is not a function
    at /Users/chris/code/affordance/app/server.js:20:3

这是执行此操作的 server.js 文件:

import path from 'path';
import { Server } from 'http';
import Express from 'express';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import routes from './routes';
import NotFoundPage from './components/NotFoundPage';

// Initialize the express server, and tell it to use ejs
const app = new Express();
const server = new Server(app);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// Tell express where the static assets are
app.use(Express.static(path.join(__dirname, 'static')));

app.get('*', (req, res) => {
  match(
    { routes, location: req.url },
    (err, redirectLocation, renderProps) => {

      if (err) {
        return res.status(500).send(err.message);
      }

      if (redirectLocation) {
        return res.redirect(302, redirectLocation.pathname + redirectLocation.search);
      }

      let markup;
      if (renderProps) {
        markup = renderToString(<RouterContext {...renderProps}/>);
      } else {
        markup = renderToString(<NotFoundPage/>);
        res.status(404);
      }

      return res.render('index', { markup });
    }
  );
});

const port = process.env.PORT || 3000;
const env = process.env.NODE_ENV || 'production';
server.listen(port, err => {
  if (err) {
    return console.error(err);
  }
  console.info(`Server running on http://localhost:${port} [${env}]`);
});

据我所知,我正在按照我在其他地方看到的方式导入它(例如,here)。

我错过了一些东西,我不知道接下来要猜测或思考什么。我做错了什么?

ps - react-router 版本为 4.0.0,匹配文档为 here

【问题讨论】:

    标签: node.js reactjs express


    【解决方案1】:

    如果您在 v4 之前使用 react-router,您的代码看起来是正确的,但 react-router v4 在整个代码库中发生了重大变化,包括服务器渲染的方法。在 v4 中,有一个专门用于服务器渲染的新组件 - StaticRouter

    您的代码在 v4 中应该如下所示:

    import path from "path";
    import { Server } from "http";
    import Express from "express";
    import React from "react";
    import { renderToString } from "react-dom/server";
    import { StaticRouter } from "react-router";
    import App from "./app";
    import NotFoundPage from "./components/NotFoundPage";
    
    // Initialize the express server, and tell it to use ejs
    const app = new Express();
    const server = new Server(app);
    app.set("view engine", "ejs");
    app.set("views", path.join(__dirname, "views"));
    
    // Tell express where the static assets are
    app.use(Express.static(path.join(__dirname, "static")));
    
    app.get("*", (req, res) => {
      // This context object contains the results of the render
      const context = {};
    
      const html = renderToString(
        <StaticRouter location={req.url} context={context}>
          <App />
        </StaticRouter>
      );
      res.status(200).send(html);
    });
    
    const port = process.env.PORT || 3000;
    const env = process.env.NODE_ENV || "production";
    server.listen(port, err => {
      if (err) {
        return console.error(err);
      }
      console.info(`Server running on http://localhost:${port} [${env}]`);
    });
    

    Here 是 EbayTech 的一篇非常好的注释文章,展示了如何使用 StaticRouter(用于服务器)和 BrowserRouter(用于客户端)设置应用

    【讨论】:

    • 刚刚这样做,不幸的是我遇到了同样的错误。我通过以下方式启动它:NODE_ENV=production node_modules/.bin/babel-node --presets react,es2015 app/server.js 这会因为任何原因而影响它吗?
    • 啊,很抱歉,我会更新我的答案。因此,您使用的匹配在 v2 和 3 中,但不在 v4 中。您在文档中链接到的匹配项用于 react-router 的路由,与 SSR 无关。如果您查看示例中的 package.json,它使用 react-router v2。您应该在 v4 的服务器端代码中使用 StaticRouter。今晚有机会我会更新我的答案。
    • 嘿哟,非常感谢。我刚刚开始记录所有的导入,匹配结果为空,我意识到你所说的正是正在发生的事情,并且正要阅读 4.0.0 的源代码。应该通读 react router api 文档,并且更仔细。太感谢了!会带我一点意识到我真正在寻找的是StaticRouter
    • 如果你想看一下,我用使用 StaticRouter 的代码更新了我的答案。
    猜你喜欢
    • 2021-10-17
    • 2011-09-28
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2023-03-04
    相关资源
    最近更新 更多