Refactoring my javascript code to node.js