Skip to content

Core Module Setup ​

This section covers all necessary setup of the core modules essential for CMS to be in running state.

Express Setup ​

All the necessary packages and the configuration required for application are configured in the (express.js) file located at (src/loaders/express.js)

let expressLoader = {};
expressLoader.init =  (app) => {
  app.use(compression());
  app.use(bodyParser.json({limit: '50mb'}));
  app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
  app.use(express.static(path.join(__dirname, '../../public')));
  app.set('views', path.join(__dirname, '../views/backend'));
  app.set('view engine', 'ejs');
  require('../config/passport')(passport);
}
module.exports = expressLoader;

IOC Container Setup ​

Similarly, the file responsible for container setup is (container.js) which is located at (src/loaders/container.js).

const awilix = require("awilix");

const { Lifetime } = awilix;

const container = awilix.createContainer({
 injectionMode: awilix.InjectionMode.PROXY
});

const containerSetup =  () => {
 container.loadModules(
   [
     "./src/controllers/**/*.js"
   ],
   {
     resolverOptions: {
       register: awilix.asClass,
       lifetime: Lifetime.transient
     },
     formatName: "camelCase"
   }
 );
}
module.exports = {
 container,
 containerSetup
};

All the controllers are resolved as class by awilix in above code snippet.

Local Function Setup ​

All the local functions are created in the (locals.js) file located at (src/loaders/locals.js).

let localFunctionLoader = {};

localFunctionLoader.init =  (app) => {

  app.locals._ = _;
  app.locals.cmsTitle = cmsTitle;
  app.locals.baseUrl = cmsUrl;

  app.locals.getDuration = (date) => {
    let newDate = date.split('/');
    let m1 = moment().format('YYYY-MM');
    let m2 = moment(newDate[1] + '-' + newDate[0]).format('YYYY-MM');
    return moment.preciseDiff(m1, m2);
  };
}
module.exports = localFunctionLoader;

Initialize Core Functions ​

const expressLoader =  require('./express');
const localFunctionLoader =  require('./locals');
const { containerSetup } = require("@container");

let init = async (app) => {
  containerSetup(app);
  expressLoader.init(app);
  localFunctionLoader.init(app);
};

module.exports = { init };

All the setup functions are called in the (index.js) file located at (src/loaders/index.js)

Bootup Application ​

The main file responsible for booting up the application is (app.js) file located at (src/app.js) where all the loader functions are initiated and express server is started.

"use strict";
const config = require('./config');
let startServer =  () => {
  const app = require("./server")();
  app.listen(config.port, err => {
    if (err) {
      console.log(err);
      return;
    }
    console.log(`Express running at port: ${config.port} -> ${config.cmsUrl}`);
  });
};
startServer();