0

I'm having a strange issue, possabily a really noob one, but for the life of me I can't get node module.exports to work for me.

Here is my route (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;

and my 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'));
});

When about.me is called, i get a "TypeError: Object # has no method 'init'" error...

Any help would be greatly appreciated!


I've just worked it out! Stupid mistake.

I'm passing a reference to the me function, not the object itself. No wonder why it doesn't exist!

4

1 回答 1

-1

我刚刚解决了!愚蠢的错误。

我正在传递对 me 函数的引用,而不是对象本身。难怪它为什么不存在!

于 2013-04-07T04:40:25.913 回答