Node.js路由错误

时间:2015-05-28 00:58:47

标签: javascript node.js express

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: {}
  });
});

但是当使用此命令DEBUG=nodecrud:* ./bin/www运行npm服务器时,我收到以下错误

/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)

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

任何想法?

1 个答案:

答案 0 :(得分:1)

您没有在此行中传递app -

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

这样做

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

并将其放在

之后
var app = express();