I am using the following code to parse json data and keep it internally in my module, for example when the server is (like npm server.js) I'm using the function parse
which parses the json files.
At the end these parsed jsons are kept in the object configObj
and I want that during user request's(via express) to get this object in the router module or other modules.
How do I achieve this?
var configObj;
parse = function () {
return fs.readFileAsync(file, 'utf8')
.then(function (res) {
return JSON.parse(res)
}).then(function (jsonObj) {
configObj = jsonObj;
return jsonObj;
})
....
})
};
module.exports = {
parse: parse,
configObj: configObj
}
The parse
function is called only once and I want to access to the configObj
many times in different modules.
require
that module in every file and node will execute it only the first time and you'll get the cached copy thereafter. – Salman