reactjs - 如何强制服务器端提供路由

标签 reactjs react-router react-starter-kit

我正在使用带有react-starter-kit和通用路由器的passport-js,并且有一些在服务器端处理的/login路由。有没有办法强制从路由 url 而不是客户端获取服务器端?

例如,如果我直接从 URL 转到/logout - 它会触发正确的服务器端注销。但是,如果我从客户端单击浏览器上的/logout 链接,则会收到页面未找到错误,因为它正在执行客户端路由,并且/logout 未在路由中定义。是否可以定义一个必须直接从服务器提供服务的路由对象?

路线/index.js

export default {

  path: '/',

  // Keep in mind, routes are evaluated in order
  children: [
    require('./home').default,
    require('./contact').default,
    require('./register').default,
    require('./termsOfUse').default,
    require('./privacyPolicy').default,
    require('./invites').default,
    require('./collection').default,
    require('./profile').default,
    require('./content').default,
    require('./notFound').default,
    // Wildcard routes, e.g. { path: '*', ... } (must go last)
  ],

  async action({ next }) {
    // Execute each child route until one of them return the result
    const route = await next();

    // Provide default values for title, description etc.
    route.title = `${route.title}`;
    route.description = route.description || '';

    return route;
  },

};

客户端.js

sync function onLocationChange(location) {
  // my workaround is to put a special case for login and logout
  // but it feels a bit of a hack since this logic should be set in the routing
  // config if possible
  if (location.pathname === '/login' || location.pathname === '/logout') {
    // break - let passport.js handle it via server-side
    window.location.reload();
    return;
  }

  ...
  currentLocation = location;

  try {
    // Traverses the list of routes in the order they are defined until
    // it finds the first route that matches provided URL path string
    // and whose action method returns anything other than `undefined`.
    const route = await UniversalRouter.resolve(routes, {
      ...context,
      path: location.pathname,
      query: queryString.parse(location.search),
    });

    // Prevent multiple page renders during the routing process
    if (currentLocation.key !== location.key) {
      return;
    }

    if (route.redirect) {
      history.replace(route.redirect);
      return;
    }

    const component = (
      <App context={context}>
        <ApolloProvider client={context.client} store={context.store}>
          {route.component}
        </ApolloProvider>
      </App>
    );

    appInstance = ReactDOM.render(
      component,
      container,
      () => onRenderComplete(route, location),
    );
  } catch (error) {
    console.error(error); // eslint-disable-line no-console

    // Current url has been changed during navigation process, do nothing
    if (currentLocation.key !== location.key) {
      return;
    }

    // Display the error in full-screen for development mode
    if (process.env.NODE_ENV !== 'production') {
      appInstance = null;
      document.title = `Error: ${error.message}`;
      ReactDOM.render(<ErrorReporter error={error} />, container);
      return;
    }

    // Avoid broken navigation in production mode by a full page reload on error
    window.location.reload();
  }
}

最佳答案

找到了here

<Link to="/someurl" target="_self">...</Link>

关于reactjs - 如何强制服务器端提供路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285834/

相关文章:

reactjs - React-router - 无法读取未定义的属性 'query'

webpack - react 入门套件页面路由中的 require.ensure 做什么

javascript - react -在主组件中添加子组件

javascript - Express 提供的客户端 React 应用程序上一致的 url 路由

javascript - react JS : Differentiate between Click and Click + Shift event

javascript - super 表达式必须为 null 或函数,而不是未定义的 - reactjs

javascript - 如何在 React 中渲染条件 HTML 元素

reactjs - React Router 仅在第二次点击后更新

javascript - 节点和浏览器在 package.json 中的不同主要入口点

reactjs - react 选择防止菜单打开 onInputChange