title: How do I factor out MySQL from API controller?
body: <p>How do I move the MySQL related function into a <code>contractsService.js</code> file?</p>
  
  <p>I'm getting an error:</p>
  
  <blockquote>
    <p>C:\code\firstdrafte2e\app.js:8 app.get('/contracts', GetContracts(req,
    res));
                                       ^</p>
    
    <p>ReferenceError: req is not defined</p>
  </blockquote>
  
  <p><strong>contractsService.js</strong></p>
  
  <pre><code>exports = function GetContracts(req, res) {
      new Promise(function (resolve, reject) {
          con.connect(function (err) {
              if (err) throw err;
              con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
                  if (err) throw err;
                  //console.log(result);
                  resolve(result);
              });
          });
      }).then(rows =&gt; res.send(rows));
  }
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>const express = require('express');
  const app = express();
  
  
      app.use(express.static('client'));
      const config = require('./config')
      var GetContracts = require('./contractsService');
  
      app.get('/contracts', GetContracts());
  
      module.exports = app;
  </code></pre>