节点从App.js导出

时间:2014-11-18 17:21:31

标签: node.js express export

我正在使用快速框架,我试图从我的app.js文件中导出的不仅仅是我的“app”。但是,我似乎无法从我的functions.js文件中访问我的方法/对象。是否可以从app.js导出多个应用程序?

以下是相关代码。

app.js

var test = "test";
//exports - trying to export serialPort so can play with it. 
module.exports = ({
  toArdu:toArdu(),
  serialPort:serialPort,
  test:test,
  app:app
});

functions.js

app_page = require('./app.js')
app = app_page.app;
toArdu = app_page.toArdu;
test = app_page.test;
serialPort = app_page.serialPort;

console.log("serialPort : " + serialPort);
console.log("this is functions :" + toArdu);
console.log("test " + test);

提前致谢。

抱歉,当我看到我的控制台时:

this is functions :undefined
serialPort : undefined
test : undefined

1 个答案:

答案 0 :(得分:0)

app.js

var test = "test";
//exports - trying to export serialPort so can play with it. 
module.exports = ({
  toArdu:toArdu(),
  serialPort:serialPort(),
  test:test,
  app:"My app"
});

function toArdu(){
    return true;
}
function serialPort(){
    return false;
}

functions.js

app_page = require('./app.js')
console.log("this is functions :" + app_page.toArdu);
console.log("serialPort : " + app_page.serialPort);
console.log("test " + app_page.test);
console.log("app " + app_page.app);

输出:

this is functions :true
serialPort : false
test test
app My App

这适用于我运行“node functions.js”