Node.js路由错误

阿姆贾德

我正在制作下面的路由文件,是node.js中的绝对初学者

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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章