我有一个奇怪的问题,可能是一个真正的菜鸟,但对于我的生活,我不能让node module.exports为我工作。
这是我的路线(routes / about.js)
var About = (function () {
function About() {
this.init();
}
About.prototype.init = function () {
console.log('init');
};
About.prototype.me = function (req, res) {
this.init();
res.json([
'jamie was here'
]);
};
return About;
})();
module.exports = About;
和我的app.js
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, about = require('./routes/about')
, http = require('http')
, path = require('path');
var app = express();
about = new about;
console.log(about);
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/me/?', about.me);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
当调用about.me时,我得到一个“TypeError:对象#没有方法'init'”错误...
非常感谢任何帮助!
答案 0 :(得分:-1)
我刚刚解决了!愚蠢的错误。
我正在传递对me函数的引用,而不是对象本身。难怪为什么它不存在!