In a Node project with many .js
files, suppose I have a file that manages expensive state: it provides a json
blob that it regularly fetches from the web in short intervals. Its data is cached and is requested internally far more than its update interval.
let provider = require('./config-provider.js');
let config = provider.get(); // returns locally cached JSON blob
Suppose the above code exists in 10 different files in my application. This is going to create 10 different instances of this updater, all making expensive web calls to update the config.
I would like to reference a single instance of this config provider across my app. However, this seems to break the modular design of Node apps.
I could always use a global object, but this is obviously frowned upon.
Another solution is to make a complex web of parent
/ child
references across my application. This also seems messy.
Is there some suggested best practice for referencing a single stateful module across the span of one's Node application?