Node.js路由错误

node.js中的绝对初学者我正在创build下面的路由文件

productCategoryRouteConfig.js

function productCategoryRouteConfig(app){ this.app = app; this.routesTable = []; this.init(); } productCategoryRouteConfig.prototype.init = function(){ this.addRoutes(); this.processRoutes(); } productCategoryRouteConfig.prototype.processRoutes = function(){ this.routesTable.forEach(function(route){ if(route.requestType === 'get') { this.app.get(route.requestUrl, route.callbackFunction) } }); } productCategoryRouteConfig.prototype.addRoutes = function(){ this.routesTable.push({ requestType: 'get', requestUrl: '/createProductCategory', callbackFunction: function(request, response){ response.render('createProductCategory', {title: "Create Product Category"}); } }); } module.exports = productCategoryRouteConfig; 

我的app.js文件在下面

 var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var productCategoryRoute = require('./routes/productCategoryRouteConfig'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use('/bower_components', express.static(__dirname + '/bower_components')); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); /******BEGIN CUSTOM ROUTES*********/ new productCategoryRoute(app) /******END CUSTOM ROUTES*********/ // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); 

但是用这个命令运行npm服务器时DEBUG=nodecrud:* ./bin/www我得到以下错误

 /home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:22 this.app.get(route.requestUrl, route.callbackFunction) ^ TypeError: Cannot read property 'get' of undefined at /home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:22:21 at Array.forEach (native) at productCategoryRouteConfig.processRoutes (/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:18:22) at productCategoryRouteConfig.init (/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:13:10) at new productCategoryRouteConfig (/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:8:10) at Object.<anonymous> (/home/sharif/Sites/node/angularmysqlnode/nodecrud/app.js:39:1) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) 

我不知道为什么会出现这个错误,请你帮我解决这个错误

任何想法?

你不是在这个行中传递app

 var productCategoryRoute = require('./routes/productCategoryRouteConfig'); 

做这个

 var productCategoryRoute = require('./routes/productCategoryRouteConfig')(app); 

并把它放在后面

 var app = express();