text: /**
   * Module dependencies.
   */
  const express = require('express');
  const compression = require('compression');
  const session = require('express-session');
  const bodyParser = require('body-parser');
  const logger = require('morgan');
  const chalk = require('chalk');
  const errorHandler = require('errorhandler');
  const lusca = require('lusca');
  const dotenv = require('dotenv');
  const MongoStore = require('connect-mongo')(session);
  const flash = require('express-flash');
  const path = require('path');
  const mongoose = require('mongoose');
  const passport = require('passport');
  const expressValidator = require('express-validator');
  const cookieParser = require('cookie-parser');
  const Project = require('./models/Project.js');
  
  /**
   * Load environment variables from .env file, where API keys and passwords are configured.
   */
  dotenv.load({
  	path: '.env.example'
  });
  
  /**
   * Controllers (route handlers).
   */
  const homeController = require('./controllers/home');
  const userController = require('./controllers/user');
  const contactController = require('./controllers/contact');
  const projectController = require('./controllers/project');
  const dashboardController = require('./controllers/dashboard');
  const setController = require('./controllers/set');
  
  /**
   * API keys and Passport configuration.
   */
  const passportConfig = require('./config/passport');
  
  /**
   * Create Express server.
   */
  const app = express();
  
  /**
   * Connect to MongoDB.
   */
  mongoose.Promise = global.Promise;
  mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
  mongoose.connection.on('error', (err) => {
  	console.error(err);
  	console.log('%s MongoDB connection error. Please make sure MongoDB is running.', chalk.red('✗'));
  	process.exit();
  });
  
  /**
   * Express configuration.
   */
  app.set('host', process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0');
  app.set('port', process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080);
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');
  app.use(compression());
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({
  	extended: true
  }));
  app.use(expressValidator());
  app.use(cookieParser());
  app.use(session({
  	resave: true,
  	saveUninitialized: true,
  	secret: process.env.SESSION_SECRET,
  	store: new MongoStore({
  		url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
  		autoReconnect: true,
  		clear_interval: 3600
  	})
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(flash());
  app.use((req, res, next) => {
  	if (req.path === '/api/upload') {
  		next();
  	} else {
  		lusca.csrf()(req, res, next);
  	}
  });
  app.use(lusca.xframe('SAMEORIGIN'));
  app.use(lusca.xssProtection(true));
  app.use((req, res, next) => {
  	res.locals.user = req.user;
  	next();
  });
  app.use((req, res, next) => {
  	if (req.user) {
  		Project.find({
  			userID: req.user._id
  		}, (err, docs) => {
  			res.locals.projects = docs;
  			next();
  		});
  	} else {
  		next();
  	}
  });
  app.use((req, res, next) => {
  	if (req.user) {
  		let id = req.user.activeProject;
  
  		Project.findOne({
  			_id: id,
  			userID: req.user._id
  		}, (err, docs) => {
  			if (docs === null) {
  				Project.findOne({
  					userID: req.user._id
  				}, (err, docs) => {
  					res.locals.activeProject = docs;
  					next();
  				});
  			} else {
  				res.locals.activeProject = docs;
  				next();
  			}
  		});
  	} else {
  		next();
  	}
  });
  app.use((req, res, next) => {
  	// After successful login, redirect back to the intended page
  	if (!req.user &&
  		req.path !== '/signin' &&
  		req.path !== '/signup' &&
  		!req.path.match(/^\/auth/) &&
  		!req.path.match(/\./)) {
  		req.session.returnTo = req.path;
  	} else if (req.user &&
  		req.path === '/account') {
  		req.session.returnTo = req.path;
  	}
  	next();
  });
  app.use(express.static(path.join(__dirname, 'public'), {
  	maxAge: 31557600000
  }));
  
  /**
   * Primary app routes.
   */
  app.get('/', homeController.index);
  app.get('/signin', userController.getLogin);
  app.post('/signin', userController.postLogin);
  app.get('/logout', userController.logout);
  app.get('/forgot', userController.getForgot);
  app.post('/forgot', userController.postForgot);
  app.get('/reset/:token', userController.getReset);
  app.post('/reset/:token', userController.postReset);
  app.get('/signup', userController.getSignup);
  app.post('/signup', userController.postSignup);
  app.get('/feedback', contactController.getContact);
  app.post('/feedback', contactController.postContact);
  app.get('/account', passportConfig.isAuthenticated, userController.getAccount);
  app.post('/account/profile', passportConfig.isAuthenticated, userController.postUpdateProfile);
  app.post('/account/password', passportConfig.isAuthenticated, userController.postUpdatePassword);
  app.post('/account/delete', passportConfig.isAuthenticated, userController.postDeleteAccount);
  
  app.get('/dashboard', passportConfig.isAuthenticated, dashboardController.getDashboard);
  
  app.get('/set/:id', passportConfig.isAuthenticated, setController.getSetActiveProject);
  
  app.get('/projects', passportConfig.isAuthenticated, projectController.getProjects);
  app.get('/projects/delete/:id', passportConfig.isAuthenticated, projectController.getDeleteProject);
  app.get('/projects/:id', passportConfig.isAuthenticated, projectController.getProject);
  app.post('/projects/:id', passportConfig.isAuthenticated, projectController.postProject);
  
  /**
   * Error Handler.
   */
  app.use(errorHandler());
  
  /**
   * Start Express server.
   */
  app.listen(app.get('port'), () => {
  	console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env'));
  	console.log('  Press CTRL-C to stop\n');
  });
  
  module.exports = app;