title: How does Node's require() resolve, such that persisted database pooling in Express works?
body: <p>I feel that I am lacking some basic understanding of how nodejs' architecture is put together so that the code below runs with no problems. I'm going to lay out a simple app. Can you guys help me with the questions at the end?</p>
  
  <p>Notes:
  I am using the mysql package <a href="https://github.com/mysqljs/mysql" rel="nofollow noreferrer">https://github.com/mysqljs/mysql</a></p>
  
  <p><code>node app.js</code> is run from the command line. </p>
  
  <p>Inside <code>app.js</code> is this:</p>
  
  <pre><code>const Express = require("express");
  const Path = require("path");
  
  const app = Express();  
  
  // Require controller modules
  var book_controller = require('../controllers/bookController');
  var author_controller = require('../controllers/authorController');
  
  router.get('/book', book_controller.books_get);
  router.get('/author', book_controller.authors_get);
  
  app.listen(5000, function(){
      console.log("Server started on port 5000");
  });
  </code></pre>
  
  <p>Inside <code>bookController</code>: </p>
  
  <pre><code>var getConnection = require('../config/mysql');
  
  // Display list of all books
  exports.book_get = function(req, res) {
      getConnection(function(err, con) {
          query = 'SELECT * FROM books';
          con.query(query, function(err, result) {
              if (err) throw err;
              con.release();
              res.render('page/authors', { result:result});
          });
      })        
  };
  </code></pre>
  
  <p>Inside <code>authorController</code>: </p>
  
  <pre><code>var getConnection = require('../config/mysql');
  
  // Display list of all books
  exports.authors_get = function(req, res) {       
      getConnection(function(err, con) {
          query = 'SELECT * FROM authors';
          con.query(query, function(err, result) {
              if (err) throw err;
              con.release();
              res.render('page/books', { result:result});
          });
      })      
  };
  </code></pre>
  
  <p>Inside <code>mysql.js</code></p>
  
  <pre><code>var mysql = require('mysql');    
  var pool = mysql.createPool({
      connectionLimit: 100,
      connectTimeout: 5000,
      acquireTimeout: 5000,
      queueLimit: 30,
      host: 'localhost',
      user: '',
      password: '',
      database: '',
      multipleStatements: true,
  });    
  
  var getConnection = function(callback) {
      pool.getConnection(function(err, connection) {
          if (err) return callback(err);
          callback(err, connection);
      });
  };
  
  pool.on('acquire', function(connection) {
      console.log('Connection %d acquired', connection.threadId);
  });
  module.exports = getConnection;
  </code></pre>
  
  <p>That's the layout. Here are the questions:</p>
  
  <ol>
  <li>How do separate files requiring the same dependency interact? Books and Author controllers both need to require and access the mysql pool, assumedly from different users. Is a new copy of the MySQL object instantiated? </li>
  <li>How does the state from the pool persist to the next connection?</li>
  </ol>