title: ExpressJS - Routing Function
body: <p>I created a separate JS file for specific server functions. Sample code as follow:</p>
  
  <p><strong>my-data.js</strong></p>
  
  <pre><code>var exports = module.exports = {};
  exports.getData = function (request, response) {
      var myData = [
          {
              "value": 1
          },
          {
              "value": 2
          },
      ];
      response.json(myData);
  };
  </code></pre>
  
  <p>In my <strong>app.js</strong>, I'm trying to call that specific function when the request has been made.</p>
  
  <p><strong>Working sample of app.js</strong></p>
  
  <pre><code>var express = require("express");
  var app = express();
  var port = process.env.PORT || 3000;
  var myData = require("./lib/my-data.js");
  
  app.engine(".html", require("ejs").__express);
  app.set("views", __dirname + "/views");
  app.set("view engine", "html");
  app.use(express.static(__dirname));
  
  // Line below is what I'm trying to achieve.
  //app.get("/get-data", myData.getData(request, response));
  
  // Working line
  app.get("/get-data", function(request, response) {
      myData.getData(request, response);
  });
  
  app.get("*", function (request, response) {
      response.render("index");
  });
  
  app.listen(port, function () {
      console.log("Listening on port " + port);
  });
  </code></pre>
  
  <p>It bothers me that the line <code>app.get("/get-data", myData.getData(request, response));</code> doesn't work while </p>
  
  <pre><code>app.get("/get-data", function(request, response) {
          myData.getData(request, response);
  });
  </code></pre>
  
  <p>does.</p>
  
  <p>What is the difference between the two approach?</p>
  
  <p>I prefer using the first one since it's clean and precise but I can't seem to make it work.</p>