Say that I have a server module called server.js
and an api module called api.js
, server.js
is the main module that i'm running.
Now, In the server, I have some functions that exports some data about the server,
In the api.js
, I want to get some data from the server exported functions by adding: var api = require('./server.js');
The problem is that I get the server.js
code running twice, first time when i'm running it, second when the api.js
running the require('./server.js')
Any suggestions to bypass this issue?
Thanks
You should do it the other way around. Something like this:
In server.js:
api = require('./api.js');
api.exportData({data1: foo, data2: bar});
In api.js:
var data1 = 'defaultvalue1', data2 = 'defaultvalue2';
exports.exportData = function(dataObject) {
data1 = dataObject.data1;
data2 = dataObject.data2;
};
// rest of your code