I’ve been playing with node.js for a while, and I’ve really come to appreciate how awesome it is. However, one thing I’m struggling to understand is how I should structure my code so that it is maintainable. Most tutorials I’ve seen on the internet have all the JS in one file, which is hardly a nice way to manage your code. I am aware there is no such thing in real-terms as a “class” in javascript, but is there a (standard) way for me to format my code for maintainability in the same way I’d structure a PHP project, for example?
Share
Afaik you can use
requireto include your own js files (containing exported methods) using:Within
someJsFile.jsyou can export methods like this:And in your main file you can address such a method using
req.someMethod()So this way you can split up your code in different files, which you
requirefrom some central js file.Here is an article explaining node.js
require