node.js 添加无法读取属性的新路由
node.js adding new routes having cannot read property
我是 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.
但是在我的 tutorRequestForm.js 文件中,我显然是这样定义我的处理程序的:
exports.selectCourse = function (req, res, next) {
res.render('selectCourseForm');
};
有什么我遗漏的吗?我认为以这种方式添加新路线应该非常简单,但在这一点上我真的很沮丧。请帮忙...
嗯,你错过了包含你的 tutorRequestForm.js 文件。我会建议您阅读一本很好的 node.js 教程以及 express.js 的教程。对我来说,显然你似乎没有理解节点背后的真正概念,特别是 express.js.
尽管如此,您应该这样做:
// All the requires are here...
var tutorRequestForm = require('pathToTheFile/tutorRequestForm');
// All the routes are defined here...
app.get('/studentLoginAfter', tutorRequestForm.selectCourse);
这应该对你有用。你基本上忘记了包含你的文件。此外,您正试图通过 routes 变量访问您的函数,该变量实际上反映了另一个文件。
我是 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.
但是在我的 tutorRequestForm.js 文件中,我显然是这样定义我的处理程序的:
exports.selectCourse = function (req, res, next) {
res.render('selectCourseForm');
};
有什么我遗漏的吗?我认为以这种方式添加新路线应该非常简单,但在这一点上我真的很沮丧。请帮忙...
嗯,你错过了包含你的 tutorRequestForm.js 文件。我会建议您阅读一本很好的 node.js 教程以及 express.js 的教程。对我来说,显然你似乎没有理解节点背后的真正概念,特别是 express.js.
尽管如此,您应该这样做:
// All the requires are here...
var tutorRequestForm = require('pathToTheFile/tutorRequestForm');
// All the routes are defined here...
app.get('/studentLoginAfter', tutorRequestForm.selectCourse);
这应该对你有用。你基本上忘记了包含你的文件。此外,您正试图通过 routes 变量访问您的函数,该变量实际上反映了另一个文件。