Javascript Node.js路由错误

Javascript Node.js路由错误,javascript,node.js,express,Javascript,Node.js,Express,node.js中的绝对初学者我正在制作下面的routes文件 productCategoryRouteConfig.js function productCategoryRouteConfig(app){ this.app = app; this.routesTable = []; this.init(); } productCategoryRouteConfig.prototype.init = function(){ this.addRoutes();

node.js中的绝对初学者我正在制作下面的routes文件

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)
/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:22
this.app.get(route.requestUrl、route.callbackFunction)
^
TypeError:无法读取未定义的属性“get”
at/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:22:21
at Array.forEach(本机)
在productCategoryRouteConfig.processRoutes(/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:18:22)
在productCategoryRouteConfig.init(/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:13:10)
在新productCategoryRouteConfig(/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:8:10)
反对。(/home/sharif/Sites/node/angularmysqlnode/nodecrud/app.js:39:1)
在模块处编译(Module.js:460:26)
在Object.Module.\u extensions..js(Module.js:478:10)
在Module.load(Module.js:355:32)
在Function.Module.\u加载(Module.js:310:12)
我不知道为什么会出现这个错误,你能帮我修复这个错误吗


有什么想法吗?

您没有在这行中输入
app
-

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

var productCategoryRoute = require('./routes/productCategoryRouteConfig')(app);
然后把它放在后面

var app = express();

谢谢@Dave Pile,我已经这么做了,现在出现了这个错误
/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:8 this.init();^TypeError:undefined不是一个函数
一般来说,我不知道编辑您的问题以删除原始错误消息并放入新的无关错误消息是一个好主意,因为这会使上面的答案变得毫无意义。可以吗@Dave Pillethaks非常感谢Dave Pill先生现在所做的工作,因为这个问题已经解决了!