content: import Express from 'express'
  import compression from 'compression'
  import mongoose from 'mongoose'
  import bodyParser from 'body-parser'
  import path from 'path'
  import IntlWrapper from '../client/modules/Intl/IntlWrapper'
  
  // Webpack Requirements
  import webpack from 'webpack'
  import config from '../webpack.config.dev'
  import webpackDevMiddleware from 'webpack-dev-middleware'
  import webpackHotMiddleware from 'webpack-hot-middleware'
  
  // Initialize the Express App
  const app = new Express()
  
  // Run Webpack dev server in development mode
  if (process.env.NODE_ENV === 'development') {
    const compiler = webpack(config)
    app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }))
    app.use(webpackHotMiddleware(compiler))
  }
  
  // React And Redux Setup
  import { configureStore } from '../client/store'
  import { Provider } from 'react-redux'
  import React from 'react'
  import { renderToString } from 'react-dom/server'
  import { match, RouterContext } from 'react-router'
  import Helmet from 'react-helmet'
  
  // Import required modules
  import routes from '../client/routes'
  import { fetchComponentData } from './util/fetchData'
  import auth from './routes/auth.routes'
  import api from './routes/api.routes'
  import posts from './routes/post.routes'
  import dummyData from './dummyData'
  import serverConfig from './config'
  
  import connectMongo from 'connect-mongo'
  import session from 'express-session'
  
  // Set native promises as mongoose promise
  mongoose.Promise = global.Promise
  
  // MongoDB Connection
  mongoose.connect(serverConfig.mongoURL, (error) => {
    if (error) {
      console.error('Please make sure Mongodb is installed and running!') // eslint-disable-line no-console
      throw error
    }
    dummyData()
  })
  
  const MongoStore = connectMongo(session)
  app.use(session({
    secret: 'keyboardcats',
    name: 'test',
    maxAge: 30,
    store: new MongoStore({ mongooseConnection: mongoose.connection }),
    proxy: true,
    resave: true,
    saveUninitialized: true
  }))
  
  // Apply body Parser and server public assets and routes
  app.use(compression())
  app.use(bodyParser.json({ limit: '20mb' }))
  app.use(bodyParser.urlencoded({ limit: '20mb', extended: false }))
  app.use(Express.static(path.resolve(__dirname, '../dist')))
  app.use('/static/modules', Express.static(path.resolve(__dirname, '../node_modules')))
  app.use('/static/resources', Express.static(path.resolve(__dirname, './resources')))
  app.use(auth)
  app.use('/api', api)
  app.use('/api', posts)
  
  // Render Initial HTML
  const renderFullPage = (html, initialState) => {
    const head = Helmet.rewind()
  
    // Import Manifests
    const assetsManifest = process.env.webpackAssets && JSON.parse(process.env.webpackAssets)
    const chunkManifest = process.env.webpackChunkAssets && JSON.parse(process.env.webpackChunkAssets)
  
    return `
      <!DOCTYPE html>
      <html>
        <head>
          ${head.base.toString()}
          ${head.title.toString()}
          ${head.meta.toString()}
          ${head.link.toString()}
          ${head.script.toString()}
          <link rel="manifest" href="/static/resources/manifest.json"
  
          ${process.env.NODE_ENV === 'production' ? `<link rel='stylesheet' href='${assetsManifest['/app.css']}' />` : ''}
          <link href='https://fonts.googleapis.com/css?family=Lato:400,300,700' rel='stylesheet' type='text/css'/>
          <link href='/static/modules/materialize-css/dist/css/materialize.min.css' rel='stylesheet' type='text/css' />
          <link href='/static/resources/font-awesome-4.7.0/css/font-awesome.min.css' rel='stylesheet' type='text/css' />
          <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
          <link rel="shortcut icon" href="http://res.cloudinary.com/hashnode/image/upload/v1455629445/static_imgs/mern/mern-favicon-circle-fill.png" type="image/png" />
        </head>
        <body>
          <div id="root">${html}</div>
          <script>
            window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}
            ${process.env.NODE_ENV === 'production' ?
            `//<![CDATA[
            window.webpackManifest = ${JSON.stringify(chunkManifest)}
            //]]>` : ''}
          </script>
          <script type='text/javascript' src='https://code.jquery.com/jquery-2.1.1.min.js'></script>
          <script src='/static/modules/materialize-css/dist/js/materialize.min.js'></script>
          <script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/vendor.js'] : '/vendor.js'}'></script>
          <script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/app.js'] : '/app.js'}'></script>
        </body>
      </html>
    `
  }
  
  const renderError = err => {
    const softTab = '&#32;&#32;&#32;&#32;';
    const errTrace = process.env.NODE_ENV !== 'production' ?
      `:<br><br><pre style="color:red">${softTab}${err.stack.replace(/\n/g, `<br>${softTab}`)}</pre>` : '';
    return renderFullPage(`Server Error${errTrace}`, {});
  }
  
  // Server Side Rendering based on routes matched by React-router.
  app.use((req, res, next) => {
    match({ routes, location: req.url }, (err, redirectLocation, renderProps) => {
      if (err) {
        return res.status(500).end(renderError(err))
      }
  
      if (redirectLocation) {
        return res.redirect(302, redirectLocation.pathname + redirectLocation.search)
      }
  
      if (!renderProps) {
        return next()
      }
  
      const store = configureStore()
  
      store.dispatch({
        type: 'SET_USER',
        user: req.user
      })
  
      return fetchComponentData(store, renderProps.components, renderProps.params)
        .then(() => {
          const initialView = renderToString(
            <Provider store={store}>
              <IntlWrapper>
                <RouterContext {...renderProps} />
              </IntlWrapper>
            </Provider>
          )
          const finalState = store.getState()
  
          res
            .set('Content-Type', 'text/html')
            .status(200)
            .end(renderFullPage(initialView, finalState))
        })
        .catch((error) => next(error))
    })
  })
  
  // start app
  app.listen(serverConfig.port, (error) => {
    if (!error) {
      console.log(`MERN is running on port: ${serverConfig.port}! Build something amazing!`) // eslint-disable-line
    }
  })
  
  export default app
  