index.js

'use strict';
/**
 * Node.js Express application index.
 *
 * @module index
 */
require('dotenv').config();
const path = require('path');
const app = require(path.join(__dirname, 'app'));
const common = require(path.join(__dirname, 'common'));

/**
 * Listen for connections.
 *
 * A node `http.Server` is returned, with this
 * application (which is a `Function`) as its
 * callback. If you wish to create both an HTTP
 * and HTTPS server you may do so with the "http"
 * and "https" modules as shown here:
 *
 *    var http = require('http')
 *      , https = require('https')
 *      , express = require('express')
 *      , app = express();
 *
 *    http.createServer(app).listen(80);
 *    https.createServer({ ... }, app).listen(443);
 *
 * @name createServer
 * @function
 * @memberof module:index
 * @inner
 * @return {http.Server}
 * @public
 */
function createServer() {
  process.once('uncaughtException', (err) => {
    console.error('uncaughtException', err);
  });

  process.once('unhandledRejection', (err) => {
    console.error('unhandledRejection', err);
    throw err;
  });

  return app.listen(common.port, () => {
    if (process.env.NODE_ENV !== 'test') {
      common.logInfo(`

    ${common.name} listening on port ${common.port}

    Backend Service API (Node.js Express API):

      (running locally)          http://localhost:${common.port}${common.productPath}
      (local docker container)   http://localhost:9090${common.productPath}
      (production)               https://${common.backendServiceDomain}${common.productPath}

    Backend Service Code Coverage Documentation:

      (running locally)          http://localhost:${common.port}${common.coveragePath}
      (local docker container)   http://localhost:9090${common.coveragePath}
      (production)               https://${common.backendServiceDomain}${common.coveragePath}

    Backend Service JavaScript API Documentation:

      (running locally)          http://localhost:${common.port}${common.docsPath}
      (local docker container)   http://localhost:9090${common.docsPath}
      (production)               https://${common.backendServiceDomain}${common.docsPath}

    Frontend Service Open API (Cloud Endpoint Extensible Service Proxy V2):

      (running locally)          http://localhost:${common.port}${common.productPath}
      (local docker container)   http://localhost:9091${common.productPath}
      (production)               https://${common.frontendServiceDomain}${common.productPath}

    `);
    }
  });
}

/**
 * Node.js Express application `http.Server`.
 *
 * @type {http.Server}
 */
const server = process.env.NODE_ENV !== 'test' ? createServer() : {};

module.exports = {
  createServer,
  server,
};