In Node.JS, how do I return an entire object from a separate .js file?

Posted by Matthew Patrick Cashatt on Stack Overflow See other posts from Stack Overflow or by Matthew Patrick Cashatt
Published on 2012-08-30T15:37:18Z Indexed on 2012/08/30 15:38 UTC
Read the original article Hit count: 233

Filed under:
|
|

Thanks for looking.

I am new to Node.js and trying to figure out how to request an object from a separate file (rather than just requesting a function) but everything I try--exports,module-exports,etc--is failing.

So, for example, if I have foo.js:

var methods = {
               Foobar:{
                        getFoo: function(){return "foo!!";},
                        getBar: function(){return "bar!!";}
               }
              };

And now I want to call a function within an object of foo.js from index.js:

var m = require('./Methods');  
function fooMain(){
  return m.Foobar.getFoo();
};

How do I do this? I have tried all sorts of combinations of exports and module-exports but they seem to only work if I call a discrete function that is not part of an object.

Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON