Managing a large list of local variables in an inherited express application can be tricky. Passing around 30 unchanging variables to res.render for every page is not ideal, especially when changes need to be made in multiple locations.
To address this issue, I decided to separate the static values from the dynamic ones. I created a separate file for the non-changing values and load it at the beginning of each route:
'use strict';
var locals =
{
indexName: 'Home',
...many more values...
};
module.exports = { locals : locals };
// in each route file
var local = require('../locals.js');
This allows me to use these global values with
res.render('sensor', local.locals);
However, adding page-specific values proved to be challenging. Attempting local.locals + {...};
did not work as expected, nor did local.locals.concat({...})
, resulting in errors like
TypeError: Object #<Object> has no method 'concat'
.
Are there any methods available to merge two objects seamlessly? Or do I need to create my own solution?
Furthermore, is this approach the most efficient way to handle global arrays? It might be better to simply call it locals
instead of local.locals
for easier access and readability.