Example #1
0
export default function routeHandler (routes, req, res, next) {
  const store = configureStore(reduxReactRouter({routes}));
  const url = req.originalUrl;

  store.dispatch(match(url, async (error, redirectLocation, routerState) => {
    if (error) {
      next(error);
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search);
    } else if (routerState) {
      req.routerState = routerState;
      req.store = store;
      next();
    } else {
      res.status(404).send('Not found');
    }
  }));
}
Example #2
0
/**
 * Example taken from redux-router documentation
 * https://github.com/acdlite/redux-router/tree/master/examples/server-rendering
 */
export default (req, res, next) => {

  const store = compose(
    reduxReactRouter({ routes, createHistory: createMemoryHistory }),
    applyMiddleware(...reduxMiddleware)
  )(createStore)(reducers);

  const query = qs.stringify(req.query);
  const url = req.path + (query.length ? `?${query}` : '');

  store.dispatch(match(url, (error, redirectLocation, routerState) => {
    if (error) {
      next(err);
    } else if (!routerState) { // 404
      console.error("No route defined in the frontend react");
      next();
    } else {
      // Pushes the data to the reducers
      store.dispatch(hydrate({
        group: req.group,
        subscriptions: req.subscriptions,
        jwtExpired: req.jwtExpired,
        jwtInvalid: req.jwtInvalid,
        leaderboard: req.leaderboard
      }));

      const initialState = serialize(store.getState());

      const html = renderToString(
        <Provider store={store} key='provider'>
          <ReduxRouter/>
        </Provider>
      );

      return res.render('pages/app', {
        layout: false,
        meta: req.meta || {},
        html,
        initialState
      });
    }
  }));
}
Example #3
0
app.use((req, res) => {
    const store = reduxReactRouter({routes, createHistory: createMemoryHistory})(createStore)(reducer)
    const query = qs.stringify(req.query)
    const url = `${req.path}${query.length ? `?${query}` : ``}`

    store.dispatch(match(url, (error, redirectLocation, routerState) => {

        if (error) {
            console.error(`Router error:`, error)
            res.status(STATUS_500).send(error.message)
        } else if (redirectLocation) {
            res.redirect(STATUS_302, redirectLocation.pathname + redirectLocation.search)
        } else if (routerState) {
            preload(store)
                .then(() =>
                    res.status(STATUS_200)
                        .send(getMarkup(store)))
        } else {
            res.status(STATUS_400).send(`Not Found`)
        }
    }))
})