text: import React from 'react';
  import { renderToString } from 'react-dom/server';
  import { Provider } from 'react-redux';
  import { createStore } from 'redux';
  import { StaticRouter } from 'react-router';
  import { renderRoutes } from 'react-router-config';
  import axios from 'axios';
  import Routes from '../../frontend/routes/ServerRoutes';
  import Layout from '../../frontend/components/Layout';
  import reducer from '../../frontend/reducers';
  import render from '../render';
  
  require('dotenv').config();
  
  const main = async (req, res, next) => {
    try {
      let initialState;
      try {
        let productList = await axios({
          url: `${process.env.API_URL}/api/products`,
          method: 'get',
        });
  
        productList = productList.data.data;
  
        initialState = {
          cart: [],
          products: productList.filter(movie => movie),
        };
      } catch (error) {
        initialState = {
          cart: [],
          products: [],
        };
      }
      const store = createStore(reducer, initialState);
      const html = renderToString(
        <Provider store={store}>
          <StaticRouter location={req.url} context={{}}>
            <Layout>{renderRoutes(Routes)}</Layout>
          </StaticRouter>
        </Provider>
      );
      const preloadedState = store.getState();
      res.send(render(html, preloadedState));
    } catch (err) {
      next(err);
    }
  };
  
  export default main;
  