text: const express = require('express');
  const fs = require('fs');
  const historyApiFallback = require('connect-history-api-fallback');
  const mongoose = require('mongoose');
  const path = require('path');
  const webpack = require('webpack');
  const webpackDevMiddleware = require('webpack-dev-middleware');
  const webpackHotMiddleware = require('webpack-hot-middleware');
  
  const config = require('../config/config');
  const webpackConfig = require('../webpack.config');
  
  const isDev = process.env.NODE_ENV !== 'production';
  const port  = process.env.PORT || 8080;
  
  const socket = require('socket.io');
  // Configuration
  // ================================================================================================
  
  // Set up Mongoose
  mongoose.connect(isDev ? config.db_dev : config.db);
  mongoose.Promise = global.Promise;
  
  const app = express();
  app.use(express.urlencoded({ extended: true }));
  app.use(express.json());
  
  // API routes
  require('./routes')(app);
  
  if (isDev) {
    const compiler = webpack(webpackConfig);
  
    app.use(historyApiFallback({
      verbose: false
    }));
  
    app.use(webpackDevMiddleware(compiler, {
      publicPath: webpackConfig.output.publicPath,
      contentBase: path.resolve(__dirname, '../client/public'),
      stats: {
        colors: true,
        hash: false,
        timings: true,
        chunks: false,
        chunkModules: false,
        modules: false
      }
    }));
  
    app.use(webpackHotMiddleware(compiler));
    app.use(express.static(path.resolve(__dirname, '../dist')));
  } else {
    app.use(express.static(path.resolve(__dirname, '../dist')));
    app.get('*', function (req, res) {
      res.sendFile(path.resolve(__dirname, '../dist/index.html'));
      res.end();
    });
  }
  
  const server = app.listen(port, '0.0.0.0', (err) => {
    if (err) {
      console.log(err);
    }
  
    console.info('>>> 🌎 Open http://0.0.0.0:%s/ in your browser.', port);
  });
  
  
  
  // Set up socket.io
  const io = socket(server);
  let online = 0;
  
  io.on('connection', (socket) => {
    online++;
    console.log(`Socket ${socket.id} connected.`);
    console.log(`Online: ${online}`);
    io.emit('visitor enters', online);
  
    // socket.on('login',() => {
    // online++;
    // socket.broadcast.emit('read',online);
    // });
    // socket.on('login',() => {
    // online--;
    // socket.broadcast.emit('read',online);
    // });
  
    socket.on('add', data => socket.broadcast.emit('add', data));
    socket.on('update', data => socket.broadcast.emit('update', data));
    socket.on('delete', data => socket.broadcast.emit('delete', data));
  
    socket.on('disconnect', () => {
      online--;
      console.log(`Socket ${socket.id} disconnected.`);
      console.log(`Online: ${online}`);
      io.emit('visitor exits', online);
    });
  });
  
  module.exports = app;
  