Access another module.exports function from the same file node.js -


to make bit more clear i'm trying achieve.

i have server running includes many module, , 1 of module use check if user role admin or not.

in server.js

   var loginapi = require('mymodule')(argstringtype),        express = require('express');     var app = express(); 

now in mymodule.js have few functions been implemented , wanted add 1 more, function doesn't need call server.js instead call once person visit url, want add 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 can see above, have 2 function in module.exports , worked fine no problem except 1 outside module.exports 1 work if don't try call getcontent, i'm trying achieve. when visit site entering url in format app.get should fire , whatever implemented do.

make sure realize each module in node.js has own scope. so

modulea:

var test = "test output string"; require('moduleb'); 

moduleb:

console.log(test); 

will output undefined.

with said, think style of module you're looking for:

server.js:

var app = //instantiate express in whatever way you'd var loginapi = require('loginmodule.js')(app); 

loginmodule.js:

module.exports = function (app) {    //setup handler   app.get( "/post/:postid", function( req, res ) {     var id = req.param('postid');     return getcontent( postid );   });    //other methods indended called more once   //any of these functions can called handler   function getcontent ( id ) { ... }    function getheader ( id ) { ... }    //return closure exposes methods publicly   //to allow them called loginapi variable   return { getcontent: getcontent, getheader: getheader }; }; 

obviously, adjust fit actual needs. there lots of ways same type of thing, fits closest original example. helps.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -