我正在制作节点程序。我想从child访问app.js参数。我在parent中使用了module.exports,在child中使用了module.parent.exports。但它没有用。我该如何解决?
(/ app.js)
var app = module.exports = express();
(/路由/ index.js)
var app = module.exports = module.parent.exports;
var port = app.get('port');
(错误讯息)
/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)
(版本)
node = v0.10.5
express = 3.2.4
答案 0 :(得分:1)
你应该使用
var app = require("../app.js");
在index.js
。
答案 1 :(得分:0)
您可以在要求期间将app
传递给路线:
(app.js)
...
var app = module.exports = express();
var routes = require('./routes')(app);
然后确保将其存储在路线中:
(路径/ 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
}
路线的任何其他出口必须是明确的出口。