我正在创建一个节点应用程序,并且我正在按照nodepad 示例来创建我的用户。但是,当我尝试打开我的 /register 页面时,我正在尝试进行路由分离并遇到问题。
我的 app.js 文件如下所示:
var express = require('express'),
app = express(),
users = require('./routes/users'),
http = require('http'),
path = require('path'),
mongoose = require('mongoose'),
mongoStore = require('connect-mongodb'),
util = require('util'),
models = require('./models'),
db,
User,
LoginToken,
Settings = { development: {}, test: {}, production: {} };
/**
* Environments
*/
app.configure('development', function() {
db = 'mongodb://localhost/db-development';
app.use(express.errorHandler({ dumpExceptions: true }));
app.set('view options', {
pretty: true
});
});
app.configure('test', function() {
db = 'mongodb://localhost/db-test';
app.set('view options', {
pretty: true
});
});
app.configure('production', function() {
db = 'mongodb://localhost/db-production';
});
console.log(db);
/**
* Models Definitions
*/
models.defineModels(mongoose, function() {
app.User = User = mongoose.model('User');
app.LoginToken = LoginToken = mongoose.model('LoginToken');
});
mongoose.connect(db);
/**
* All environments
*/
app.configure(function() {
app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ url: db, secret: 'secret' }));
});
...
// Sessions
app.post('/sessions', sessions.index);
app.get('/login', sessions.new);
app.del('/sessions', loadUser, sessions.del);
// Users
app.get('/register', users.register);
app.post('/users.:format?', users.registerprocess);
...
然后,我的死记硬背文件看起来像这样:
exports.register = function(req, res) {
var user = new User();
res.render('users/new.jade', { flash: req.flash(),
title: 'Register',
bodyClass: 'registration',
user: user});
};
exports.registerprocess = function(req, res) {
var user = new User(req.body.user);
function userSaveFailed() {
req.flash('error', 'Account creation failed');
res.render('users/new.jade', { flash: req.flash(),
title: 'Register',
bodyClass: 'registration',
user: user });
}
user.save(function(err) {
if (err) return userSaveFailed();
req.flash('info', 'Your account has been created');
// emails.sendWelcome(user);
switch (req.params.format) {
case 'json':
res.send(user.toObject());
break;
default:
req.session.user_id = user.id;
res.redirect('/');
}
});
};
当我尝试转到 /register 时,出现以下错误:
ReferenceError: User is not defined
at module.exports.register (/Users/mmellado/workspace/project/routes/users.js:2:18)
at callbacks (/Users/mmellado/workspace/project/node_modules/express/lib/router/index.js:161:37)
at param (/Users/mmellado/workspace/project/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/mmellado/workspace/project/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/mmellado/workspace/project/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/mmellado/workspace/project/node_modules/express/lib/router/index.js:33:10)
at next (/Users/mmellado/workspace/project/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/Users/mmellado/workspace/project/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:49:5)
at next (/Users/mmellado/workspace/project/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.logger (/Users/mmellado/workspace/project/node_modules/express/node_modules/connect/lib/middleware/logger.js:156:5)
这让我假设由于某种原因,从路由文件中找不到用户模式。关于为什么会发生这种情况的任何想法?