HI,
I have an existing nodejs application that uses expressjs and is built with multiple modules. I am trying to dockerize the same. Its designed in such a way that the expressjs server is configured as a module(app-server) in the app_modules & loaded using app.js below.
var config = require("config");
var fs = require('fs');
load_modules(require("path").join(process.cwd(), `/${config.module_path}`));
function load_modules(module_path) {
try {
fs.readdirSync(module_path).forEach(file => {
var path = `/${module_path}/` + file;
fs.stat(path, (err, stat) => {
if(err){
console.error(`Err:load_modules() while loading ${file} module \n error caused by ${err.message}` );
}
else if (stat && stat.isDirectory()) {
load_modules(path);
} else {
require(path);
}
});
});
} catch (err) {
console.error("Err:load_modules()->error while loading modules caused by:" + err.message);
}
}
This app.js file scans the app_modules directory & loads the requisite modules which includes an app_server directory which has an index.js file as below
var config =require('config');
var express = require('express');
var bodyParser = require('body-parser');
var app_server =express();
var uuid = require('node-uuid');
var httpContext = require('express-http-context');
// support json encoded bodies
var urlencodedParser=bodyParser.urlencoded({ extended: true })
app_server.use(urlencodedParser);
app_server.use(bodyParser.json());
app_server.use(httpContext.middleware);
var logger =require('./logger');
var httpStatus = require('http-status-codes');
var cors = require('cors');
var fs = require('fs');
var running_env=process.env['NODE_ENV'];
var https = require('https');
var _ =require('lodash');
var http = require('http');
var api_response=require('./api_response');
var ErrorResponse =api_response.Error;
http.createServer(app_server).listen(config.port);
/**set express into app_server*/
app_server.express=express;
/**set */
app_server.api_response= api_response;
app_server.logger =logger;
var loggingMiddleware=(req,res,next)=>{
httpContext.set(config.logging_req_id_key,uuid.v1());
next();
}
var errorHandler=(err,req,res,next)=>{
if (res.headersSent) {
return next(err);
}
logger.error("Error caused while processing request caused by-> "+err.message);
var errResp =new ErrorResponse(httpStatus.INTERNAL_SERVER_ERROR,'Something went wrong, please contact to system admin.');
if(err instanceof ErrorResponse|| typeof(err) =='ErrorResponse'){
errResp =err;
}
res.status(httpStatus.INTERNAL_SERVER_ERROR).json(errResp);
};
app_server.use(loggingMiddleware);
app_server.use(errorHandler);
logger.info("SME app module has initialized successfully.");
module.exports=app_server;
This runs smoothly when I don’t use docker & just run node app.js .
I am able to make api calls to the node server.
But when run using docker it just exists with code 1, without any errors.
What I understand is that it executes app.js & ends execution, does not keep the express server running to service requests.
This is my docker-compose file
node:
image: node-app
build: .
ports:
- 9100:9100
links:
- mysql:mysql
depends_on:
- mysql
command: node app.js
If I modify my docker-compse to this
node:
image: node
build: .
ports:
- 9100:9100
links:
- mysql:mysql
depends_on:
- mysql
command: node app_modules/app_server/index.js
The server comes up running. I can see it when I run docker ps, but most of the modules are not loaded & naturally not servicing my api requests too.
I am now wondering if even my node-apps design is correct? How do I get this working in docker?