Please review this app.js file
const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const http = require('http');
const _ = require('lodash');
const cors = require('cors');
const configurations = require("./config.json");
const LockManager = require('../../LockManager.js');
const lockManager = new LockManager('medics_lab_interface_server.lock');

// Import Logger and MongoDBService
const createLogger = require('../../services/Logger');
const connectToDatabase = require('../../services/MongoDBService');

// Create the application
const app = module.exports = express();

// Setup Logger
const logger = createLogger('MedicsLabInterfaceServer');

// Attach logger to the app
app.logger = logger;

/**
 * Initialize the Medics Lab Interface Server.
 * This function handles the setup of the application, including database connection, middleware,
 * error handling, and HTTP server creation.
 */
async function initializeApp() {
    try {
        // Check for lock
        await lockManager.checkLock();

        // Create the lock with a timeout and retries of specified intervals
        await lockManager.createLock(Infinity, 3);

        // Connect to MongoDB using MongoDBService
        await connectToDatabase(configurations.mongodbURL, logger);

        // Add middleware necessary for REST APIs
        app.use(cors());

        app.use(methodOverride('X-HTTP-Method-Override'));

        app.use(bodyParser.json({
            limit: '10mb'
        }));

        app.use(bodyParser.urlencoded({
            limit: '10mb',
            extended: true,
            parameterLimit: 10000
        }));

        // CORS Support
        app.use(function (req, res, next) {
            logger.info('Processing request:', req.url);
            if (req.body && Object.keys(req.body).length > 0) {
                logger.info('Request Body:', req.body);
            }

            res.header('Access-Control-Allow-Origin', '*');
            res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
            res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH');

            if (req.method === 'OPTIONS') {
                return res.status(200).json({});
            } else {
                next();
            }
        });

        // Error handling middleware
        app.use(function (err, req, res, next) {
            if (err.name === 'UnauthorizedError') {
                res.status(401);
                res.json({
                    "message": err.name + ": " + err.message
                });
            } else {
                logger.error(err.stack);
                res.status(500);
                res.json({
                    "message": "Internal Server Error"
                });
            }
        });

        // Load the models
        app.models = require('./models/index');

        // Load the Routes dynamically
        createRoutes();

        process.on('unhandledRejection', (error, promise) => {
            logger.error('Unhandled Promise Rejection:', error);
        });

        // Create an HTTP service.
        const httpPort = configurations.serverPort;
        logger.info(`HTTP server is listening on port: ${httpPort}`);
        http.createServer(app).listen(httpPort);

    } catch (error) {
        logger.error('Initialization error:', error);
        process.exit(1);
    }
}

const createRoutes = () => {
    const routes = require('./routes');
    _.each(routes, function (controller, route) {
        app.use(route, controller(app, route));
    });
};


/**
 * Handles application termination gracefully by removing the lock and logging a message.
 */
async function handleTermination() {
    try {
        // Remove the lock to allow future instances
        await lockManager.removeLock();

        // Log termination message and exit the process
        logger.info('Application is terminated gracefully.');
        process.exit();
    } catch (error) {
        // Log any errors that occur during termination
        logger.error('Error during termination:', error);
        process.exit(1);
    }
}

// Clean up the lock on process exit or termination signals
process.on('exit', handleTermination);
process.on('SIGINT', handleTermination);
process.on('SIGTERM', handleTermination);

// Call the async function to initialize the app
initializeApp();