0

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.

4
  • Just require() them, like everything else. What problem are you having? Commented Jun 20, 2013 at 13:45
  • You could look into running something like backbone.js on your server. It structures your code into models, routers, and views. Commented Jun 20, 2013 at 13:48
  • @SLaks as noted above, I require the script but then how do I access the functions within said script? The sample code above throws an error. EDIT: As in, class1.js has a function called function1 inside of it. How do I call this function from my main script? class1.function1(); does not work. Commented Jun 20, 2013 at 13:53
  • Are you looking for a way to route web request to specific functions in different files? Say: GET /clients triggers function listClients in clientController.js and/or POST /product trigger function addProduct in productController.js ? If so I think I might help, otherwise please desconsider. Commented Jun 20, 2013 at 13:57

2 Answers 2

1

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 object:

// first-module.js

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


// main.js

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

console.log(first.hello);
console.log(first.num);
Sign up to request clarification or add additional context in comments.

4 Comments

Quick bit of clarification: If exports.hello is a function that calls num (another function), but main.js only calls hello(), do I need to add num() to exports as well? Or do I only add the functions I'm actively calling from main.js?
Only the functions you are calling. If you don't export them they will be private, which means not accessible from outside your module but exported functions inside the module can still call it just fine.
gotcha. This means I'll have to modify my classes so that the first function that is called returns the treated object when all the sibling functions are done. Thanks for your help!
I don't think so. You can just exports.MyClass = MyClass; and then use them that way.
1

You need to add functions to the exports object in class1.js.
require("./class1") will return this object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.