这个ReactRouter.match()实现有什么问题?

时间:2017-03-14 22:12:23

标签: node.js reactjs 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

1 个答案:

答案 0 :(得分:5)

如果您在v4之前使用了反应路由器,那么您的代码看起来是正确的,但是反应路由器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(用于客户端)设置应用程序