问题
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