Express node.js添加具有无法读取属性的新路由

Express node.js添加具有无法读取属性的新路由,express,Express,我是Node的新手,所以如果这真的很愚蠢,请容忍我。我只是想为几个新页面添加我自己的路线,这些页面是基于我从一本书中获得的示例应用程序构建的。下面是我的app.js文件的一些代码: var express = require('express'), routes = require('./routes'), http = require('http'), path = require('path'), mongoose = require('mongoose'),

我是Node的新手,所以如果这真的很愚蠢,请容忍我。我只是想为几个新页面添加我自己的路线,这些页面是基于我从一本书中获得的示例应用程序构建的。下面是我的app.js文件的一些代码:

var express = require('express'),
    routes = require('./routes'),
    http = require('http'),
    path = require('path'),
    mongoose = require('mongoose'),
    models = require('./models'),
    dbUrl = process.env.MONGOHQ_URL || 'mongodb://@localhost:27017/blog',
    db = mongoose.connect(dbUrl, {safe: true}),
以下是所有路线的定义:

app.get('/', routes.index);
app.get('/studentLogin', routes.user.studentLogin);
app.post('/studentLogin', routes.user.checkID);
app.get('/studentLoginAfter', routes.tutorRequestForm.selectCourse);
app.get('/login', routes.user.login);
app.post('/login', routes.user.authenticate);
app.get('/logout', routes.user.logout);
app.get('/admin', authorize, routes.article.admin);
app.get('/post', authorize, routes.article.post);
app.post('/post', authorize, routes.article.postArticle);
app.get('/articles/:slug', routes.article.show);
studentLoginAfter路线是我试图添加的路线,但每次我添加它时,都会出现如下错误:

app.get('/studentLoginAfter', routes.tutorRequestForm.selectCourse);
                                              ^
TypeError: Cannot read property 'selectCourse' of undefined.
exports.selectCourse = function (req, res, next) {
    res.render('selectCourseForm');
};
// All the requires are here...
var tutorRequestForm = require('pathToTheFile/tutorRequestForm');
// All the routes are defined here...
app.get('/studentLoginAfter', tutorRequestForm.selectCourse);
但在我的tutorRequestForm.js文件中,我显然是这样定义我的处理程序的:

app.get('/studentLoginAfter', routes.tutorRequestForm.selectCourse);
                                              ^
TypeError: Cannot read property 'selectCourse' of undefined.
exports.selectCourse = function (req, res, next) {
    res.render('selectCourseForm');
};
// All the requires are here...
var tutorRequestForm = require('pathToTheFile/tutorRequestForm');
// All the routes are defined here...
app.get('/studentLoginAfter', tutorRequestForm.selectCourse);

有什么我错过的吗?我认为以这种方式添加新路线应该是非常简单的,但在这一点上我真的很沮丧。请帮助…

好吧,您没有包含tutorRequestForm.js文件。我建议您阅读一篇好的node.js教程,还有一篇关于express.js的教程。对我来说,很显然,您似乎没有了解node背后的真正概念,尤其是express.js

然而,您应该这样做:

app.get('/studentLoginAfter', routes.tutorRequestForm.selectCourse);
                                              ^
TypeError: Cannot read property 'selectCourse' of undefined.
exports.selectCourse = function (req, res, next) {
    res.render('selectCourseForm');
};
// All the requires are here...
var tutorRequestForm = require('pathToTheFile/tutorRequestForm');
// All the routes are defined here...
app.get('/studentLoginAfter', tutorRequestForm.selectCourse);

这应该对你有用。你基本上忘了包括你的文件。此外,您试图通过routes变量访问函数,该变量实际上反映了另一个文件。

感谢您的回答,非常感谢。我忘了提到我已经将tutorRequestForm.js放在routes文件夹下,所以它确实包含在内。我已经弄明白了,这完全与路由问题无关,原来是我随机呈现的页面,路由需要传递一些对象。。。至少我是这么想的。。。所以,我重新将路由重定向到一个新的空页面,现在一切都好了。我真的很感谢你的帮助!好吧,酷,它现在对你有效了!我很乐意帮助你