title: How to get result from function in other file in node.js
body: <p>Trying to call database query from other javascript file in NodeJS.</p>
  
  <p>Sample database file:</p>
  
  <pre><code>function addUser(user) {
  
    connection.connect(function (err) {
      if (err) {
          console.error('Error connecting: ' + err);
      }
      console.log('Connected as id ' + connection.threadId);
    });
  
    var sql = "INSERT INTO `mobile`.`main` (`userId`) VALUES (?);"
    var inserts = [user];
  
  
    connection.query(sql, inserts, function (error, results) {
      console.log('query');
      if (error) {
          return error;
      } else {
          console.log('Success Query');
          return results;                
      }
    });
    connection.end(function (err) {
      if (err) {
          console.error('Error connecting: ' + err);
      }
      console.log('Connection closed!');
    });
  }
  
  module.exports = addUser;
  </code></pre>
  
  <p>Sample main.js file:</p>
  
  <pre><code>app.get('/api/mysql/:user', function (req, res) {
    var user = req.params.user;
    addUsers(user)
    res.json({
      SQLResp: 'Query succes',
      result: addUsers.result
    });
  });
  </code></pre>
  
  <p>How to get the result from the first file and use it as a response in the main js?</p>