I'm trying to set GLOBAL variable available across all modules of Node.js app.
module.exports = app => {
app.get('/config', async (req, res) => {
.....
const { data } = await axios.get('.....')
app.locals.MY_CONFIG = data.config
Then in other module:
module.exports = app => {
app.post('/getData', async (req, res) => {
.....
const { data } = await axios.get('.....')
if (app.locals.MY_CONFIG.prop1 === true) {
.....
} else {
.....
}
Will this line cause memory leaks every time the route is called?
app.locals.MY_CONFIG = [data.config]
in other words will this cause an issue:
app.locals.MY_CONFIG = [data.config]
app.locals.MY_CONFIG = [data.config]
app.locals.MY_CONFIG = [data.config]
app.locals.MY_CONFIG = [data.config]
...
By doing app.locals.MY_CONFIG = [data.config]
you're overwriting the previous value of app.locals.MY_CONFIG
. Assuming you aren't holding another reference to it somewhere, that old value is now eligable for garbage collection. In other words, there is no memory leak in the snippet provided in the question.