Global Variables alternative

给你一囗甜甜゛ 提交于 2019-12-14 02:59:18

问题


I want to create an array that is accessible to all the .js files. This array must also be updateable, and the updated changes must be reflected in all the other files using that array.

I have read that using a GLOBAL variable is not a good method, but what is the alternative?

//file 1 which contains my global variable
var mySharedArray = ['helo']; 
module.exports = {
mySharedArray: mySharedArray,
get: function(){
    console.log(mySharedArray);
} 
}    

//file 2 where i am pushing new-data 
router.get('/workerequestest',function(req,res){
queue.push("moreData");
require('../config/globalQueue').get();
require('../config/startWorkerQueue');
res.send(200,'message recieved');
});

//file 3 where i am checking the updated values
var interval = setInterval(function() {
var Queue = require('../config/globalQueue').mySharedArray;
require('../config/globalQueue').get();
}, 10000,'hello' ,'checking for New processes');

回答1:


Proper modularity in node.js does not use actual globals as that makes actually taking advantage of reusable modules more complicated and makes for possible conflicts with other globals.

One design pattern that can be used instead is to put the shared data in its own module and then just require() in that module wherever needed.

// shared-data.js
var mySharedArray = [...];

module.exports = {
    mySharedArray: mySharedArray
}

Then, in each module that wants to have access to that array, you can do this:

// someothermodule.js

var sharedArray = require('./shared-data.js').mySharedArray;


// this modification will change the main array and will be seen by
// all modules that are sharing it
sharedArray.push("moreData");

This works because when arrays are assigned in Javascript, they are not copied, but instead a pointer to the original array is shared. So, any modifications to the assigned value is just pointing at the original object and thus changes the original array. Thus, all modules would have access to the same original array.


I just tested this concept in my own three files and it works fine. Here are my three files:

Server File:

// shared-server.js
var express = require('express');
var app = express();
var server = app.listen(80);
var shared = require('./shared-var');
require('./shared-interval.js')

var cntr = 0;

app.use(express.static('public'));

app.get("/test", function(req, res) {
    shared.mySharedArray.push(++cntr);
    shared.get();
    res.send(200,'message received');
});

Shared Variable File:

//shared-var.js
var mySharedArray = ['helo']; 
module.exports = {
    mySharedArray: mySharedArray,
    get: function(){
        console.log(mySharedArray);
    } 
}

Interval File:

//shared-interval.js
var interval = setInterval(function() {
    require('./shared-var').get();
}, 1000);

When I run this, it outputs the value of mySharedArray every second. Each time I go to http://localhost/test with my browser, it adds a new value to mySharedArray and I see that in the console. This is the behavior I would expect.




回答2:


You could put them in a separate module that gets required in whatever module you need it in, else you could use a seperate storage service such as redis or memcached to store your array of data.



来源:https://stackoverflow.com/questions/34862600/global-variables-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!