I am trying to build an express.js app and I want to pass some data to my main layout. For example, a list of navigation links that I use on every page. I know how to pass data
A better solution instead of using app.locals
will be to set a middleware that runs before each route.
app.use((req,res,next) => {
res.locals.navLinks = [
// array data
];
next()
});
Will run before any HTTP request and will set the navLinks before every route.
this will cause the navLinks
to be valid only for the request lifetime and won't polute app.locals
. (which will persist throughout the life of the application).