I'm making node program. I want to access app.js parameter from child. I used module.exports in parent and module.parent.exports in child. But it didn't work. How can I fix it?
(/app.js)
var app = module.exports = express();
(/routes/index.js)
var app = module.exports = module.parent.exports;
var port = app.get('port');
(error message)
/Users/satoshi/Google Drive/node/testoy2/routes/index.js:7
var port = app.get('port');
^
TypeError: Object #<Object> has no method 'get'
at Object.<anonymous> (/Users/satoshi/Google Drive/node/testoy2/routes/index.js:7:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/satoshi/Google Drive/node/testoy2/app.js:8:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
(version)
node = v0.10.5
express = 3.2.4
You should use
var app = require("../app.js");
in your index.js.
You can pass the app to the routes during require:
(app.js)
...
var app = module.exports = express();
var routes = require('./routes')(app);
Then make sure to store it in routes:
(routes/index.js)
var expApp = undefined;//This variable will hold the express app
var routes = module.exports = function(app){
expApp = app;//Now you have access to express
}
Any other exports from routes must be explicit export.