我是节点&表达和我尝试了不同的变化,无法弄清楚为什么。如果我将app.get('/')映射到contentHandler,那么它可以正常工作。但是,如果我将app.get('/)映射到contentHandler.displayWelcomePage,那么我会收到上述错误。谢谢你的期待。
这是我的结构: app.js
路由/ index.js
路由/ content.js
app.js
var express = require('express')
, app = express()
, routes = require('./routes')
, http = require('http')
, path = require('path')
, mongoose = require ('mongoose')
, db = mongoose.connection
;
db.on ('error', console.error);
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use (app.router);
routes(app, db);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
路由/ index.js
var contentHandler = require('./content');
//this is what I need to add, see my notes below var aHandler = new contentHandler();
module.exports = exports = function (app, db) {
console.log ('inside routes/index.js');
app.get ('/', contentHandler.displayWelcomePage);
//instead of using contentHandler, use aHandler.displayWelcomePage
};
路由/ content.js
function contentHandler (req, res, db) {
console.log ('Inside contentHandler');
res.render('index', {layout: false, title: "Jade is three."});
this.displayWelcomePage = function (req, res) {
return res.render('index', {layout: false, title: "Welcome to Jade"});
};
};
module.exports = contentHandler;