Use closure to store and access state in different files

what your code does can be explained like this :

require('./store') "is" a function, which expects a request object. therefore you can effectively call it in your app.use part.

But in router.js you are still calling this very same function which also expects a request object. it acts like : let req = null. since you call it without the request object it will return nothing.

What you want to achieve is a problem not fully solved in nodejs (for real). or at best not stable for every package i needed. for exemple this concept has never worked for me with npm mysql. hence stoping me from using it at all. be stable or be gone :p

Here is a full explaination of how you can efectively create a store like the one you tried to do.

https://www.freecodecamp.org/news/async-local-storage-nodejs/

maybe things has gotten better recently. Please ping me if you manage doing it.

i tried some libs : continuation-local-storage cls-hooked async_hooks