0

I am creating an application using express and I ran into an issue using module.exports. I have a route that will handle checking out a user a creating a pdf based on their order. To keep things modular I have been separating my route functionality into modules and requiring them when necessary, for example:

Route1.js

module.exports = function(req, res){
   // route functionality
};

routes/index.js

Route1 = require('./Route1');
router.post('/api/v1/route1', Route1);

This has been working great to keep things organized, but I created a route with a lot of complex functionality and there seems to be an issue with caching. The first time I call the route it works fine, but the second time I call it it gets stuck in an infinite loop because one of the variables from the last request is persisting and it never breaks out of the loop because it is greater than the number that it needs to equal for the loop to exit. I am even resetting all of the variables at the beginning of the function that is passed through module.exports.

To fix the problem I am no longer exporting the route functionality and instead passing it directly to the route. Here is an example:

Works everytime

router.post('/api/v1/dostuff', function(req, res){
    var count = 0;

    var doSomething = function(){
       // if count === something else break
       // else add one to count and run this function again
    }
});

Works the first time

do_something.js

module.exports = function(req, res){
    var count = 0;

    var doSomething = function(){
       // if count === something else then break out and do something else
       // else add one to count and run this function again

       // This works the first time but the second time 
       // count is persisting and it is already greater than 
       // the number I am checking against so it 
       // never breaks out of this function
    }
};

routes/index.js

var doSomething = require('./do_something.js');

// Only works as expected the first time the call is made
router.post('api/v1/dosomething', doSomething);

So why when using module.exports does my function only work as expected once? I am thinking is has something to do with node module caching.

pizzarob
  • 11,711
  • 6
  • 48
  • 69
  • There shouldn't be an issue if the code is exactly the same and there is nothing else being done with the `module` object in the `do_something.js` file. The only issue there could be is if you were editing the `doSomething` variable that you have required in `routes/index.js`. For some debugging ideas, check out [this answer](http://stackoverflow.com/a/16060619/3696076) and some of the others from that question. – chrisbajorin Aug 19 '15 at 18:00
  • @cdbajorin Thats what I thought too, but something does in fact happen when it is the same exact code. – pizzarob Aug 19 '15 at 22:53
  • The only other thing I can think of is maybe there is an issue with recursion (I'm assuming recursion based on the limited description)? Maybe you could try to switch it into a while loop. Again, this should't be an issue, but it's another debug trail to follow. – chrisbajorin Aug 20 '15 at 15:35

1 Answers1

0

I'm quite not sure about module caching but I have some function in my app that work quite similar to yours. So I would like you to try export module like this

something.controller.js

exports.doSomething = function(req, res){
  // your code
}

and index.js

var controller = require('./something.controller.js');

router.post('/api/v1/dosomething', controller.doSomething);

Hope this help

murnax
  • 1,222
  • 1
  • 10
  • 13
  • I think this will give me the same result, although i could try when i have time. I tried an object oriented approach earlier something like module.exports = { ... init: function()... }. This would be the same as doing exports.init = function(); I got the same result. Variables were persisting and not being reset. – pizzarob Aug 19 '15 at 17:20