Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript react路由器服务器端的错误处理_Javascript_Express_Reactjs_React Router - Fatal编程技术网

Javascript react路由器服务器端的错误处理

Javascript react路由器服务器端的错误处理,javascript,express,reactjs,react-router,Javascript,Express,Reactjs,React Router,我正在用react路由器和express开发同构应用程序。我正在服务器端渲染中使用react路由器。我希望有自定义的错误页面,它将在服务器端错误、渲染错误或当然-找不到时发送。但我在连接这些点时遇到了问题。 以下是我的路线: <Route component={Index} path="/"> <IndexRoute component={Home} /> <Route component={Chart} path="chart" /> <R

我正在用
react路由器
express
开发同构应用程序。我正在服务器端渲染中使用react路由器。我希望有自定义的错误页面,它将在服务器端错误、渲染错误或当然-找不到时发送。但我在连接这些点时遇到了问题。 以下是我的路线:

<Route component={Index} path="/">
  <IndexRoute component={Home} />
  <Route component={Chart} path="chart" />
  <Route component={Login} path="login" />
  <Route component={Error} path="error" /> //path="*" takes care only for not found,
                                             and it still sends the client a 200 status code
</Route>
getHtml(renderProps)
使用
上的
react
renderToString()
生成html

我想以某种方式路由到错误组件,该组件将使用路由器上下文(或其他方式)访问错误代码,因此它将显示适当的错误


有什么想法吗?

我可能错了,但试试看:

if (!renderProps) {
  //404 not found
  let error = new Error();
  error.message = "Not Found";
  error.status = 404;
  res.status(error.status).end(someErrorHtml); // <- I think you are missing this
  return next(err);
}
if(!renderProps){
//404找不到
让错误=新错误();
error.message=“未找到”;
error.status=404;

res.status(error.status).end(someErrorHtml);//我在服务器端渲染中找到了404状态的解决方案

按如下方式配置路由:

<Route component={Index} path="/">
  <IndexRoute component={Home} />
  <Route component={Chart} path="chart" />
  <Route component={Login} path="login" />
  <Route component={Error} path="*" notFound />
</Route>
app.use((req, res, next) => {
  const location = createLocation(req.url);
  const css = [];
  match({ routes, location }, (err, redirectLocation, renderProps) => {
    if (err) {
      //500 internal error
      return next(err);
    }
    if(redirectLocation){
      //react-router redirect, no problems here
      return res.redirect(redirectLocation.path + redirectLocation.query);
    }
    if (!renderProps){
      //404 not found
      let error = new Error();
      error.message = "Not Found";
      error.status = 404;
      return next(err);
    }

    const notFound = renderProps.routes.filter((route) => route.notFound).length > 0;
    res.status(notFound ? 404 : 200);

    res.end(getHtml(renderProps));
  }
}

我的问题是获取
Index->Error
嵌套组件的html,如routes上所述。谢谢,React Router也更新了他们的文档来回答这个问题,请参见此处:。他们几乎是在说您写的:“您也可以检查RenderOps.components或RenderOps.routes以查找您的”未找到““如果您使用的是全面覆盖的路由,请分别发送组件或路由,并发送一个404,如下所示。”您能在这里帮助我处理错误吗?谢谢:)
app.use((req, res, next) => {
  const location = createLocation(req.url);
  const css = [];
  match({ routes, location }, (err, redirectLocation, renderProps) => {
    if (err) {
      //500 internal error
      return next(err);
    }
    if(redirectLocation){
      //react-router redirect, no problems here
      return res.redirect(redirectLocation.path + redirectLocation.query);
    }
    if (!renderProps){
      //404 not found
      let error = new Error();
      error.message = "Not Found";
      error.status = 404;
      return next(err);
    }

    const notFound = renderProps.routes.filter((route) => route.notFound).length > 0;
    res.status(notFound ? 404 : 200);

    res.end(getHtml(renderProps));
  }
}