Creating a "controller" script for Node.js


Creating a "controller" script for Node.js

I’m creating a program in Node.js
. I’m pretty new to programming anything other than small javascript functions for websites so please bear with me if my terminology/ideas are totally wrong.

I originally had the entire program in one giant (~500 line) script file. Several people suggested I split it up into separate classes, where each class only has one ‘job’ to complete. I like this idea as it has helped me really streamline my code and make it more modular and manageable.

My issue is: How do I access these classes from a central file?

For instance, pretend I have 3 classes, in 3 separate javascript files, all containing 3 functions each. I want to access and pass data to/from all of these from one central "controller" script. What’s the best way to do this? Can I just require
it into a variable, then access the script’s functions like so?

var class1 = require('./class1.js');

class1.function1(); // call the first function contained in class1.js

Is such a thing even possible? Again, totally new to this.

Problem courtesy of: Jascination


NodeJS supports CommonJS modules
. A CommonJS module provides three global variables: module
, exports
and require
You can export your API by adding to the exports
object and require
these files just like other node modules (add ./
to indicate that it is relative to the current file), assign it to a variable and access the values you added to that files exports

// first-module.js

exports.hello = 'world';
exports.num = 23;

// main.js

var first = require('./first-module');


Solution courtesy of: Daff


Creating a "controller" script for Node.js

轻描淡写,微软提前 4 年停止 Win8 系统应用更新支持


Orientation supported loading overlay for React Native



Creating a "controller" script for Node.js