1

To make it a bit more clear to what I'm trying to achieve.

I have a server running which includes many module, and one of the module use to check if the user role is an admin or not.

in Server.js

   var loginAPI = require('myModule')(argStringType),
       express = require('express');

   var app = express();

Now that in the myModule.js I have few functions that already been implemented and just wanted to add one more, but this function really doesn't need to be call from the server.js instead it will be call once the person visit the URL, so I want to add something like this to the myModule.js

in myModule.js

app.get( "/post/:postid", function( req, res ) {
  var id = req.param('postid');
  return getContent( postid );
});



// Module.exports
module.exports = function ( arg ) {

  return {

    getContent: function ( id ) { },

    getHeader: function ( id ) { };
};

So as you can see from the above, I have two function which is in the module.exports and they worked fine no problem except the one which is outside the module.exports that one work if I don't try to call the getContent, but that is what I'm trying to achieve. When someone visit the site by entering the URL in that format the app.get should be fire and do whatever is implemented to do.

10
  • Couldn't you just export it as well? I don't see the point of the question otherwise. return { exportName: exportName, [...] and after require: loginAPI.exportName(arg) Commented May 20, 2013 at 2:48
  • @FabrícioMatté The thing is that I want this app.get to always be active without being call, so once they do the require it will be use right away. Commented May 20, 2013 at 2:49
  • I see. Still not entirely sure of what you're trying to achieve, but from your code seems like you'd like to add a method to the global object. Commented May 20, 2013 at 2:50
  • @FabrícioMatté I'm so sorry with my question is very confuse I know that, but here is a briefly explaination of what I'm trying to achieve. We have a server that include a lot of the module and one of the module that I'm trying to implement got some functionality, and so I want to add one more function into it, but the problem is that this function needs to be always active meaning that it doesn't need to be call only when they visited that URL then it will be active. When they visited that site they have to access the other function to get some detail as well. Commented May 20, 2013 at 3:18
  • 1
    I second what Fabrício said, if you're wanting to use exportName, why not export it? module.exports.exportName = function (arg) {...}; and then call it by using loginAPI.exportName(arg);. If that's not what you're asking, you might need to rework your question with expanded descriptions and examples. Commented May 20, 2013 at 3:51

1 Answer 1

6

Make sure you realize each module in Node.js has its own scope. So

ModuleA:

var test = "Test output string";
require('ModuleB');

ModuleB:

console.log(test);

Will simply output undefined.

With that said, I think this is the style of module you're looking for:

server.js:

var app = //instantiate express in whatever way you'd like
var loginApi = require('loginModule.js')(app);

loginModule.js:

module.exports = function (app) {

  //setup get handler
  app.get( "/post/:postid", function( req, res ) {
    var id = req.param('postid');
    return getContent( postid );
  });

  //other methods which are indended to be called more than once
  //any of these functions can be called from the get handler
  function getContent ( id ) { ... }

  function getHeader ( id ) { ... }

  //return a closure which exposes certain methods publicly
  //to allow them to be called from the loginApi variable
  return { getContent: getContent, getHeader: getHeader };
};

Obviously, adjust to fit your actual needs. There are lots of ways to do the same type of thing, but this fits closest with your original example. Hopefully that helps.

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

2 Comments

It make more sense from your answer and I do understand it better in your way, but the only problem I have and concern about is that I don't want to modify the whole module that I'm trying to implement. It is an open source project that I'm contributing to, so they only want me to add something into it, but not changing their codes. Anyway, if it's not possible that way I will try and see if I can use yours and implement it, or maybe ask them if they are okay for me to change their codes. Thanks!
If it's an open source project, then I would definitely pose your question directly to the maintainers/community and see how they would like it handled. They may very well already have a paradigm in place which would work for you. Good luck.

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.