2

Diving into node, I have a question on how an included script, can access the main's script methods or variables

For instance, say that I have a logger object initiated in the main script and I include another js file, which needs access to the logger. How can I do that, without injecting the logger to the included script.

//main node script

var logger = require('logger'),
    app = require("./app.js")

//app.js

function something(){
  //access logger here !!!!!!!
}

exports.something = something

Hope it is clear

Thanks

2 Answers 2

3

Try doing:

//main node script

var logger = require('logger'),
    app = require("./app.js")(logger);

//app.js

var something = function (logger){
  logger.log('Im here!');
}

module.exports = exports = something;

Edit:

If you want to split your main app's code into different files, on your main script file you can do something like: (This is how I'm splitting my main app.js into different sections)

// main app.js

express = require('express');
...

/* Routes ---------------------*/
require('./config/routes')(app);

/* Socket IO init -------------*/
require('./app/controllers/socket.io')(io);

/* Your other file ------------*/
require('./path/to/file')(app);
...


// config/routes.js

module.exports = function(app){

    app.configure(function(){
        app.get('/', ...);
        ...
    }
}


// app/controllers/socket.io.js

module.exports = function(io){
  // My custom socket IO implementation here
}

// ...etc

Edit 2:

Your function can also return a JS object, in case you want to do something on the main app.js with a custom script.

Example:

// main app.js

...

/* Some controller ---------------------*/
var myThing = require('./some/controller')(app);

myThing.myFunction2('lorem'); // will print 'lorem' on console
...


// some/controller.js
// Your function can also return a JS object, in case you want to do something on the main app.js with this require 

var helperModule = require('helperModule');

module.exports = function(app){

  var myFunction = function(){ console.log('lorem'); }

  // An example to export a different function based on something
  if (app.something == helperModule.something){
    myFunction = function() { console.log('dolor'); }  
  }

  return {
    myFunction: myFunction,
    myFunction2: function(something){
      console.log(something);
    }
  }
}

You can also simply export a function or an object containing functions, without sending any parameter like this:

// main app.js

...
var myModule = require('./path/to/module');
myModule.myFunction('lorem'); // will print "lorem" in console
...


// path/to/module.js

module.exports = {
  myFunction: function(message){ console.log(message); },
  myFunction2: ...
}

Basically, whatever you put inside module.exports is what gets returned after the require() function.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. No way without injecting the dependency? It can become messy if you need to access several modules
You have to manually send the objects that you wish to keep consistent on the splitted files (Like the app object), but you can also call var myModule = require('myModule') on the include files without calling them on the main app.js script. That way, you can only require the modules that will be used on each splitted file.
Thanks. Can you elaborate on the module.exports = function().. statement? I understand that this way you can configure the service. But if you also want to expose a method of the included script, to the main, how would that be done?
Sure. *Look at Edit 2
1

module.exports is what is returned when you require the file. in @DavidOliveros example, that's a function that takes app or io as parameters. the function is executed right after the require takes place.

If you want to expose a method of the included script to the main, try this:

// child.js
module.exports = function(app){
  var child = {};
  child.crazyFunction = function(callback){
    app.explode(function(){
      callback();
    });
  };

  child.otherFunction = function(){};
  return child;
};

// app.js
var express = require('express');
var app = module.exports = express();
var child = require('./child.js')(app);
app.get('/', function(req, res){
  child.crazyFunction(function(){
    res.send(500);
  });
});

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.