-1

我正在制作节点程序。我想从孩子访问 app.js 参数。我在 parent 中使用了 module.exports,在 child 中使用了 module.parent.exports。但它没有用。我该如何解决?

(/app.js)

var app = module.exports = express();

(/routes/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
4

2 回答 2

1

你应该使用

var app = require("../app.js");

在你的index.js.

于 2013-05-20T21:15:33.623 回答
0

您可以app在 require 期间将 传递给路由:
(app.js)

...
var app = module.exports = express();
var routes = require('./routes')(app);

然后确保将其存储在路由中:
(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
}

路由的任何其他导出必须是显式导出。

于 2013-05-20T22:28:18.193 回答