I originally had a working application in nodejs which had all of its code in one file, split out into discrete chunks using constructors and IIFEs. In order to provide sensible decoupling and separation, I've been trying to refactor the program so that each chunk lives in a different file, but I've run into a problem.
The program is split into three core chunks:
A datastore constructor that initialises to connect to Redis and returns a redis client and dataStore object.
function CreateDataStore(storePort, storeUrl, passwd) {
var client
, datastore;
//create the redis client
//create the datastore
//exports
return {
"redisClient": client,
"dataObject": createSiteUpdater("")
};
}
var dataStore = CreateDataStore("xxxx","xxxx","xxxx");
var client = dataStore.redisClient;
var testResults = dataStore.dataObject;
A web crawler constuctor which uses the redis client and stores data in the datastore object.
function CreateWebCrawler(){
//do some stuff
//initialise robots.txt parser
return function() {
//crawl a page
//add page data to testResults object
}
}
A web server Constructor which reads the same datastore object and sends the data out to a front-end client.
function CreateWebServer() {
//do some stuff
//initialise an express webserver
//initialise socket.io
//send testResults object out over a socket.
}
This all worked fine when they were in the same file, but now that I want to refactor them into three separate files, I'm confused about how they're going to talk to each other.
Under the new architecture both the web crawler module and the web server module "require" the data store module, which exports a dataStore object. What I need to happen is for any changes made by the web crawler module to its dataStore object to be replicated in the version of the dataStore object available to the web server module.
I've tried looking through the all the nodejs documentation, and also done a lot of googling, but I can't for the life of me work out if an object that is returned by a commonjs module's export statement is the same object on the heap when it's returned separately to two different other modules higher up the dependency graph. Can anyone explain to me if this is true, and ideally provide me a link to the correct documentation to understand the behaviour properly?