text: import Express from 'express';
  import cookieParser from 'cookie-parser';
  import React from 'react';
  import ReactDOM from 'react-dom/server';
  import config from './config';
  import favicon from 'serve-favicon';
  import compression from 'compression';
  import httpProxy from 'http-proxy';
  import path from 'path';
  import createStore from './redux/create';
  import ApiClient from './helpers/ApiClient';
  import getDataDependencies from './helpers/getDataDependencies';
  import Html from './helpers/Html';
  import PrettyError from 'pretty-error';
  import http from 'http';
  import SocketIo from 'socket.io';
  
  import {ReduxRouter} from 'redux-router';
  import createHistory from 'history/lib/createMemoryHistory';
  import {reduxReactRouter, match} from 'redux-router/server';
  import {Provider} from 'react-redux';
  import qs from 'query-string';
  import getRoutes from './routes';
  import getStatusFromRoutes from './helpers/getStatusFromRoutes';
  import { load as loadAuth } from './redux/modules/auth';
  
  const pretty = new PrettyError();
  const app = new Express();
  const server = new http.Server(app);
  const proxy = httpProxy.createProxyServer({
    target: 'http://localhost:' + config.apiPort,
    ws: true
  });
  
  app.use(compression());
  app.use(favicon(path.join(__dirname, '..', 'static', 'favicon.ico')));
  
  app.use(require('serve-static')(path.join(__dirname, '..', 'static')));
  
  app.use(cookieParser());
  // Proxy to API server
  app.use('/api', (req, res) => {
    proxy.web(req, res);
  });
  
  // added the error handling to avoid https://github.com/nodejitsu/node-http-proxy/issues/527
  proxy.on('error', (error, req, res) => {
    let json;
    console.log('proxy error', error);
    if (!res.headersSent) {
      res.writeHead(500, {'content-type': 'application/json'});
    }
  
    json = { error: 'proxy_error', reason: error.message };
    res.end(JSON.stringify(json));
  });
  
  app.use((req, res) => {
    if (__DEVELOPMENT__) {
      // Do not cache webpack stats: the script file would change since
      // hot module replacement is enabled in the development env
      webpackIsomorphicTools.refresh();
    }
    const client = new ApiClient(req);
  
    const store = createStore(reduxReactRouter, getRoutes, createHistory, client);
  
    function hydrateOnClient() {
      res.send('<!doctype html>\n' +
        ReactDOM.renderToString(<Html assets={webpackIsomorphicTools.assets()} store={store}/>));
    }
  
    if (__DISABLE_SSR__) {
      hydrateOnClient();
      return;
    }
  
    const query = qs.stringify(req.query);
    const url = req.path + (query.length ? '?' + query : '');
  
    const afterAuth = () => {
      store.dispatch(match(url, (error, redirectLocation, routerState) => {
        if (redirectLocation) {
          res.redirect(redirectLocation.pathname + redirectLocation.search);
        } else if (error) {
          console.error('ROUTER ERROR:', pretty.render(error));
          res.status(500);
          hydrateOnClient();
        } else if (!routerState) {
          res.status(500);
          hydrateOnClient();
        } else {
          Promise.all(getDataDependencies(
            routerState.components,
            store.getState,
            store.dispatch,
            routerState.location,
            routerState.params
          )).then(() => {
            const component = (
              <Provider store={store} key="provider">
                <ReduxRouter/>
              </Provider>
            );
            const status = getStatusFromRoutes(store.getState().router.routes);
            if (status) {
              res.status(status);
            }
            res.send('<!doctype html>\n' +
              ReactDOM.renderToString(<Html assets={webpackIsomorphicTools.assets()} component={component} store={store}/>));
          }).catch((err) => {
            console.error('DATA FETCHING ERROR:', pretty.render(err));
            res.status(500);
            hydrateOnClient();
          });
        }
      }));
    };
  
    store.dispatch(loadAuth()).then(afterAuth, afterAuth);
  });
  
  if (config.port) {
  
    server.listen(config.port, (err) => {
      if (err) {
        console.error(err);
      }
      console.info('----\n==> ✅  %s is running, talking to API server on %s.', config.app.name, config.apiPort);
      console.info('==> 💻  Open http://localhost:%s in a browser to view the app.', config.port);
    });
  } else {
    console.error('==>     ERROR: No PORT environment variable has been specified');
  }
  