1

I want to get an argv from my command line when I am going to start my server and then I want to set it as a constant for a module.

For example I want to define my log file path from commandline:

My starter.js looks like:

var optimist = require("optimist");
var server = require("./start_server");
var argv = optimist.describe('logpath', 'logptah for info').argv;
server.init({logpath:argv.logpath});

My start_server.js looks like:

 var restify = require('restify');
 var server = restify.createServer({
 name: 'dummy-project'
 });
 module.exports.logpath = null;
 function init(args){
 server.listen(1234, function() {
 console.log("inside logs");
 console.log(args.logpath);
 module.exports.logpath = args.logpath;
 console.log('%s listening at %s', server.name, server.url);
 });
 };
 module.exports.init = init;
 var fun = require('./common/loghandler');

My loghandler.js looks like:

var server = require('./../start_server');
console.log("inside log handler");
var LOGPATH = server.logpath;
console.log(LOGPATH);

When I am running node starter.js --logpath='../../custom/info.txt' I am not getting the logpath inside my loghandler.

Seems logpath handler is called before the server.listen.

The console output looks like:

node starter.js --logpath='../../custom/info.txt'
inside log handler
null
inside log handler 
../../custom/info.txt
dummy-project listening at http://0.0.0.0:1234

How I can overcome it and pass my log path as command line argument?

Thanks in advance.

j0k
22.8k28 gold badges81 silver badges90 bronze badges
asked Sep 11, 2013 at 13:52

1 Answer 1

1

Your init() function is executed after starter.js uses require('./start_server'). When you use require(), the file and all its dependencies are loaded. That means during this process, you also executed require('./common/loghandler'), which completes before server.init() is run in starter.js. Since server.logpath hasn't been set by this time, you get a null value.

Aside from that, module.exports is set at require time and changing the values later have no effect. To fix the problems you're having, you should avoid using functions before your application has fully loaded, and put return values in your modules.

answered Sep 12, 2013 at 1:22
Sign up to request clarification or add additional context in comments.

2 Comments

How I can avoid this "To fix the problems you're having, you should avoid using running functions before your application has fully loaded, and put return values in your modules." I am passing args inside init of a module and then I want to use the value outside init through out the module.
You could move require('./common/loghandler'); into the init() function, and change loghandler.js to a function. Then, during startup, pass the value from init() like this: require('./common/loghandler')(args.logpath);

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.