title: How do I close mysql connection after each request with Express?
body: <p>I have a router file like this:</p>
  
  <pre><code>import express from 'express';
  import health from './health';
  import surface from './surface';
  
  const router = express.Router();
  
  router.use('/health', health);
  router.use('/surface', surface);
  
  router.get('*', (req, res) =&gt; {
    res.status(404);
    res.json({
      message: 'Uknown API endpoint'
    });
  });
  
  export default router;
  </code></pre>
  
  <p>Within each route, I am using the NPM <code>mysql</code> package to create a connection and then end it right before I call <code>res.send()</code>. IIRC creating a connection and ending it per query would be a bad idea.</p>
  
  <p>I tried doing middleware like:</p>
  
  <pre><code>router.use((req, res, next) =&gt; {
    next();
    mySingletonWrappingMysql.connection.end();
  });
  </code></pre>
  
  <p>Given that my routes are doing async things, the <code>endConnection</code> call is happening too soon. <code>next</code> isn't really async so I can't do <code>await next</code> and I'm not even sure this would be the right way/place to do it. This example is using a singleton because I also wasn't sure of how to properly set a new instance to be use within the route. <code>req.param</code> seemed like an option that isn't optimal cause that seems to be for special query param vars.</p>