text: const mongoose = require('mongoose');
  const util = require('util');
  
  // config should be imported before importing any other file
  const config = require('./config/config');
  const app = require('./config/express');
  const io = require('./server/socket');
  
  const debug = require('debug')('server:index');
  
  // make bluebird default Promise
  Promise = require('bluebird'); // eslint-disable-line no-global-assign
  
  // plugin bluebird promise in mongoose
  mongoose.Promise = Promise;
  
  // connect to mongo db
  const mongoUri = config.mongo.host;
  mongoose.connect(mongoUri, {
  	useNewUrlParser: true,
  	useUnifiedTopology: true,
  	useFindAndModify: false,
  });
  mongoose.connection.on('error', (err) => {
  	console.error(`Unable to connect to database: ${mongoUri}\n${err}`);
  	process.exit(1);
  });
  
  // print mongoose logs in dev env
  if (config.mongooseDebug) {
  	mongoose.set('debug', (collectionName, method, query, doc) => {
  		debug(`${collectionName}.${method}`, util.inspect(query, false, 20), doc);
  	});
  }
  
  // module.parent check is required to support mocha watch
  // src: https://github.com/mochajs/mocha/issues/1912
  if (!module.parent) {
  	// listen on port config.port
  	const server = app.listen(config.port, () => {
  		console.info(`server started on port ${config.port} (${config.env})`); // eslint-disable-line no-console
  	});
  	io.attach(server);
  }
  
  module.exports = app;
  