title: How do I call one controllers method in another controller in Node
body: <p>So I have one controller with a method defined as</p>
  
  <p>controller1.js</p>
  
  <pre><code> router.get('/:id/:field', function(req,res){
     var regex = /action|protocol|ip|port|direction|dip|dport|signature/;
       if (regex.test(req.params.field)){
         get(req,res,function(r){
           var field = req.params.field;
           res.status(200).send(r[field]);
         });
       } else {
           res.status(404).send("Signature Field Does Not Exist");
       }
   });
  
   function get(req, res, cb){
     MongoClient.connect(url, function(err, db) {
       if (err){
         console.error("Could not connect to database: %s",err);
         res.sendStatus(500);
       } else {
           var _id = req.params.id
           var collection = db.collection("signatures");
           var uniqueID = {"_id":_id};
           var cursor = collection.find(uniqueID); 
           cursor.hasNext(function (err, r) {
             if (err) {console.log(err);}
             else {
               cursor.next(function(err,r) {
                 if (r == null){
                   res.status(404).send("Signature not found");
                 } else {
                     cb(r);
                     db.close();
                 }
               });    
             }    
           });
       }
     });
   }
  module.exports = router
  </code></pre>
  
  <p>This works well in its own class and I can call it from outside via localhost. I want to be able to use both of these in another controller. So in another file</p>
  
  <p>controller2.js</p>
  
  <pre><code>var controller1 = require("./controller1.js");
  router.get('/', function(req,res){
    controller1.get(req,res,cb(r){
        res.status(200).send(r);
    });
  });
  </code></pre>
  
  <p>When I attempt to call get in controller2.js I get Error: Route.get() requires callback functions but got a [object Object]. I am sure it is not a database error or connection error of any sort just an error in calling controller1 function from controller2. I've tried changing the header in controller1 to be </p>
  
  <pre><code>router.get = function(req,res,cb){
      ....
  });
  </code></pre>
  
  <p>This makes the get in controller1 unable to call the function.</p>