title: Storing state in a Node/Express application
body: <p>I'm writing a Node/Express app (my first) and need help persisting what feels like a global variable across page loads; but using a global variable feels wrong.</p>
  
  <p>For the purposes of the question, the site is essentially 3 different table views on the same data. The data in the tables is taken from 3 separate MongoDB tables, merged together with a function, and displayed (using Pug, iterating over the object I just created).</p>
  
  <p>The 3 different views are just different Pug templates iterating over the same object.</p>
  
  <p>I have middleware which generates this object from MongoDB and stores it on <code>res.locals</code>, but I don't want to re-generate it by default every time the user selects a different view. The data hasn't changed, and this feels wasteful.</p>
  
  <p>I want to be able to set some sort of variable, <code>dataNeedsToBeUpdated</code>, and if <code>true</code> then the function inside my middleware will actually do the work and regenerate the table; if <code>false</code>, it'll just skip the operation.</p>
  
  <p>If the user performs one of my update operations, I'll set <code>dataNeedsToBeUpdated = true</code>, redirect, middleware will fire, and the data will refresh before the next page load.</p>
  
  <p>How do I do this properly?</p>