0

I have looked at the solution here and while it is the right solution to passing an argument to a module, my requirement is to pass command like arguments from bin/www to app.js to a module. This is a project created in WebStorm, which has bin/www as the startup script.

I have a traditional NodeJS project structure where the app starts at bin/www which call app.js and app.js has the router modules.

The startup script is bin/www which I cannot change and I want to pass an argument (path to a config file) to a router module.

What is the best way to achieve this without significantly changing the file structure?

app.js has the following structure:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index')(path_config_file);
var usersRouter = require('./routes/users');
var app = express();


module.exports = app;

As I have shown I want to pass path_config_file to routes/index module, which will be passed as a command line argument.

I cannot start app.js because the project has existing CI/CD and I am not able to change that process.

I need to be able to pass the argument as node bin/www path_config_file and get it over to app.js.

Thank you.

Community
  • 1
  • 1
madu
  • 5,232
  • 14
  • 56
  • 96

1 Answers1

0

Basically, what you need is to read the process.argv var where those command line arguments are saved. Please look at the answers on the next post:

How to pass command line arguments on node