2
In a Nodejs application with Express/EJS, I need to keep an updated list and I would like that whenever the user accesses my root route, that is the INDEX page, he could see this updated list. To summarize I put together the script below, which is responsible for updating the client list (but it doesn’t have to be the way I put it together...):
Client list update script:
// clients_list.js ============================================================
const tls = require('tls');
var clients = [];// Essa será a minha lista de clientes <<<===========
var aa = [{name: "aaaaaaaa"}, {name: "bbbbbbbb"}];
const server = tls.createServer(options, (myObj) => {
myObj.on('beginEvent', (anotherObj) => {
// Adiciona um cliente a cada evento
clients.push(anotherObj);
})
myObj.on('endEvent', (anotherObj) => {
// Remove um cliente a cada evento
clients.splice(clientes.indexOf(anotherObj), 1);
})
});
module.exports = { server, clients, aa };
EXPRESS "app.js" file, where I want the list to be accessible and always updated:
// app.js do EXPRESS ==========================================================
const app = express();
// vários outros requires...
const indexRouter = require('./routes/index');
// Importação do servidor "serverCLIENTS.server" e da lista "serverCLIENTS.clients"
const serverCLIENTS = require('./clients_list');
// um monte de coisas....
// Minha rota raiz
app.use('/', indexRouter); // <<<========= PRETENDO QUE ESTA ROTA EXIBA A LISTA "serverCLIENTS.clients"
// um monte de coisas....
module.exports = app;
QUESTION: How do I make this list "clients" remain visible and up to date in my application?
Will the way is to make one middleware of this my script clients_list.js
?
As lists are passed by Javascript reference, any file you require from it will be accessing the same list. If your app js. require from the list, and add an item, this item will also appear for another file that does require. But there is a point to be considered when using this method: If your list is kept in a variable, it will only exist while the server is standing. When restarting the server, the list that was in volatile memory will be lost. Was that the question? Sorry it was not very clear.
– Andre
My doubt is the same, but I didn’t understand how to make this list "updateable" everywhere I do the
require
. For example, in theapp.js
, I can access the client list throughindexRouter.clients
, OK! However, as I make that same list visible also in the fileindex.js
for example. If I make arequire
inside the pageindex.js
, a new instance of the list will be created and not a note to the existing list inapp.js
. Do you agree? I don’t know if I can make myself understood now, but can you help me I am very grateful.– wBB
No, a new instance will not be created.
clients
inindex.js
will be the same instance ofclients
inclients_list.js
.– Andre
I think there’s something else I’m not seeing... at the beginning of
client_list.js
, if for example I includevar aa = [{name: "aaaaaaaa"}, {name: "bbbbbbbb"}];
, i can list this data on the index page. However, the variableclients
ofclient_list.js
is updated in an asynchronous process, ie when I run therequire clients_list.js
in theindex.js
for example, this client list does not yet have any item. Over time it is being incremented. Only when I update the index page, no item is shown.– wBB
I edited the script and include the variable var aa = [{name: "Aaaaaaaaaa"}, {name: "bbbbbbbb"}]; at first. This data I can list on the index page. Already the variable clients, even after including items in it I can not list them in the index, because it will only be updated after the server is created through the line
const server = tls.createServer(options....
. After this piece of code is executed I will no longer be able to list the contents of the variable because the index page has already been loaded. I think that’s the problem that keeps me from seeing the updated list.– wBB
@user140828, I managed to make it work without changing anything of the code I posted initially, but I could not find the problem that prevented it from working before. But as you said, really Nodejs DOES NOT CREATE another instance of the Object when doing
require
of the same JS file in multiple scripts (it’s in the documentation included), as long as an OBJECT is exported, as I did. If a FUNCTION is exported, then the story is different. Well at least it served to me to study a lot more things. Thank you!– wBB