server.js

// Setting environment variables
process.env.JWT_SECRET = ''
process.env.NODE_ENV = 'production'

var NODE_ENV = {
  production: 'Database points to mongodb web and UI fetches minified files',
  debug: 'Database points to mongodb web and UI fetches unminified files',
  nodb: 'App runs without database',
  local: 'Database points to mongodb local and UI fetches unminified files'
}

// Setting error handler
process.on('uncaughtException', function (err) {
  console.log(err)
})

// set up ======================================================================
const express = require('express')
const errorHandler = require('express-error-handler')
const app = express() // create our app w/ express
const mongodb = require('mongodb') // import the mongodb native drivers.
const MongoClient = mongodb.MongoClient
const Logger = mongodb.Logger
const port = process.env.PORT || 7575 // set the port
const morgan = require('morgan')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const jwt = require('jsonwebtoken')
const MongoStore = require('connect-mongo')(session)
const dbConfig = require('./app/config/dbConfig') // load the database config
const routes = require('./app/routes/routes.js')
const Utility = require('./app/lib/modules/Utility')

// Add response headers
app.use(function (req, res, next) {
  res.set('Access-Control-Allow-Methods', 'GET, POST')
  res.set('Access-Control-Allow-Headers', 'Content-Type')
  next()
})

// configuration ===============================================================
app.use(errorHandler({ dumpExceptions: false, showStack: false }))
app.use(express.static(__dirname + '/public'))
app.use(morgan('dev')) // log every request to the console
app.use(
  bodyParser.urlencoded({
    extended: 'true'
  })
)

// parse application/x-www-form-urlencoded
// parse application/vnd.api+json as json
app.use(bodyParser.json())
app.use(
  bodyParser.json({
    type: 'application/vnd.api+json'
  })
)

// override with the X-HTTP-Method-Override header in the request
app.use(methodOverride('X-HTTP-Method-Override'))
app.use(cookieParser())
app.use(
  session({
    secret: '--- OMMITTED ---',
    saveUninitialized: true,
    resave: true,
    // using store session on MongoDB using express-session + connect
    store:
      process.env.NODE_ENV != 'nodb'
        ? new MongoStore({ url: dbConfig.db.url })
        : ''
  })
)

// We need to work with "MongoClient" interface in order to connect to a mongodb server.
var routeOptions = {}
if (process.env.NODE_ENV != 'nodb') {
  MongoClient.connect(dbConfig.db.url, function (err, db) {
    if (err) {
      console.log('Unable to connect to the mongoDB server. Error:', err)
    } else {
      console.log('Database connection established to', dbConfig.db.type)

      // routes
      console.log('Initializing routes')
      routeOptions.database = db
      routes(app, routeOptions)
      console.log('Server initialization completed')
    }
  })
} else {
  routes(app, routeOptions)
}

// listen (start app with node server.js) ======================================
app.listen(port, function () {
  console.log('App listening on: ', port)
  console.log('Time: ' + Utility.getDateTime())
})