title: How to synchronously load a csv file into memory before handling HTTP requests
body: <p>I am trying to write a simple express/node.js app that responds to GET requests using data found in a csv file. I would like to read this csv file to generate a javascript object (essentially a key-value mapping), and then make that generated map available for the HTTP request handling logic in the controller.</p>
  
  <p>I wrote a module that reads the csv files and exports the desired objects, but I'm not sure how to ensure:</p>
  
  <ol>
  <li>This operation completes and the objects actually exist before HTTP requests are handled</li>
  <li>The file operation is performed only a single time when the server starts up and not once per request incurring massive overhead</li>
  </ol>
  
  <p>How can I organize my code to meet these goals in the context of an express app?</p>
  
  <p>This is how I am processing the CSV file:</p>
  
  <pre><code>var myMap = {};
  
  fs.createReadStream('filename.csv')  
    .pipe(csv())
    .on('data', (row) =&gt; {
      // Build javascript object
      myMap[row['key']] = row['value'];
    })
    .on('end', () =&gt; {
      console.log('Done.');
    });
  
  // Does this work?
  module.exports =  myMap;
  </code></pre>