我一直在寻找几天没有,而且我觉得我在这里错过了这个概念的一部分...我对node.js很新,我试着打电话给来自我主要课程中不同模块的方法per-say ...
这是代码..
inputReader.js
(function() {
var dir = './views/'; // Declare the directory to be scanned
var data = {} // Create array object for storage
fs.readdir(dir, function(err, files) {
if (err) {
throw err;
}
var c = 0; // Declare a var c = 0; initial condition of a for loop
files.forEach(function(file) {
c++; // Increment a counter in the for-loop condition
fs.readFile(dir+file, 'utf-8', function(err, string) {
if (err) {
throw err;
}
if ( 0 === -3) {
data[file] = string; // Throws into data object string of contents within the file being read
console.log(data); // We only need this to test using console (the contents being stored)
}
});
});
});
module.exports.getData = function() {
return data();
}
}());
以下是我试图在app.js中调用它的方式
var inputReader = require('./inputReader').inputReader;
app.get('/', function(req, res){
res.send(inputReader.getData());
});
app.listen(3000);
console.log('Listening on port 3000');
我的预测如果我这样做了,我的localhost页面会显示我指定应用程序读取的文件夹中文件的内容; ./views/ ..但显然,我做错了,因为我得到的错误是:
TypeError:在c:\ Users \ Brian \ documents \ visualizer \ app.js:21:24回调时无法调用未定义的方法'getData'(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js:164:37)at param(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js:138:11)pass(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js:145:5)在Router._dispatch(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js: 173:5)在Object.router(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js:33:10)下一步(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ node_modules \ connect \ lib \ proto.js:193:15)在Object.expressInit [作为句柄](c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ middleware.js:30: 5)在Object.query [作为句柄]的下一个(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ node_modules \ connect \ lib \ proto.js:193:15)(c:\ Users \ Brian \文件\可视\ n ode_modules \表达\ node_modules \连接\ lib中\中间件\ query.js:44:5)
如果有人可能指出我正确的方向或向我解释我做错了什么,那将不胜感激
谢谢! (很抱歉长篇大论......)
答案 0 :(得分:2)
写出来的几种不同方式:
// inputReader.js
module.exports.getData = function() {
return data();
}
// app.js
var inputReader = require('./inputReader'); // inputReader contains getData
inputReader.getData();
或
// inputReader.js
module.exports.getData = function() {
return data();
}
// app.js
var inputReader = require('./inputReader').getData; // inputReader is now getData
inputReader();
或
// inputReader.js
var theModule = {
getData : function() {
return data();
}
}
module.exports = theModule;
// app.js
var inputReader = require('./inputReader');
inputReader.getData();
或
// inputReader.js
var theModule = function() { /* constructor */ };
theModule.prototype.getData = function() {
return data();
};
module.exports = theModule;
// app.js
var inputReader = require('./inputReader');
new inputReader().getData();