javascript - save object in module and access it from different modules - Stack Overflow

I am using the following code to parse json data and keep it internally in my module, for example when

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.

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.

Share Improve this question edited Sep 1, 2015 at 5:25 Alex Andrei 7,2833 gold badges29 silver badges44 bronze badges asked Aug 3, 2015 at 10:29 07_05_GuyT07_05_GuyT 2,88715 gold badges48 silver badges92 bronze badges 1
  • If I understood your problem correctly, then you could simply require that module in every file and node will execute it only the first time and you'll get the cached copy thereafter. – Salman Commented Aug 12, 2015 at 6:47
Add a ment  | 

2 Answers 2

Reset to default 6

You could use something like node-persist:

var storage = require('node-persist');
storage.setItem('config', configObj);
console.log(storage.getItem('config'));

If you use express best way is app.set: when you require your parse module function save result for example by:

app.set("parse.configObj",configObj)

and get it when need:

app.get("parse.configObj")

Or you can use require.cache scope after require:

server.js:

var parse = function () {
        return fs.readFileAsync(file, 'utf8')
            .then(function (res) {
                return JSON.parse(res)
            }).then(function (jsonObj) {
                if (typeof require.cache.persist === "undefined") {
                  require.cache.persist = {};
                }
                require.cache.persist.configObj = jsonObj;
                return jsonObj;
            })
    })
};
module.exports = { parse: parse }

app.s

var parse = require('./server.js').parse();

routes/index.js

console.log( require.cache.persist.configObj );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744254564a4565332.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信