NodeJS和Express3的新功能......完成我在以下网站找到的教程:
http://www.scotchmedia.com/tutorials/express/authentication
这似乎很有帮助,但我似乎在获得超级优秀时遇到了一些困难。
这是测试:
describe('POST /signup', function () {
it('should redirect to "/account" if the form is valid', function (done) {
var post = {
givenName: 'Barrack',
familyName: 'Obama',
email: 'Bob@bob.com',
password: 'secret'
};
request(app)
.post('/signup')
.set('Content-Type', 'application/json')
.send(post)
.expect(302)
.end(function (err, res) {
should.not.exist(err);
// confirm the redirect
res.header.location.should.include('/account');
done();
});
});
});
/注册路由由以下人员处理:
exports.signup = function(req,res){
console.log("Calling Signup")
req.onValidationError(function (msg) {
//Redirect to `/signup` if validation fails
console.log("Validation Failed: " + msg + " " + req.param('email'));
return res.redirect('/signup');
});
req.check('password', 'Please enter a password with a length between 4 and 34 digits').len(4, 34);
req.check('givenName', 'Please enter your first name').len(1);
req.check('familyName', 'Please enter your last name').len(1);
req.check('email', 'Please enter a valid email').len(1).isEmail();
// If the form is valid craete a new user
var newUser = {
name: {
givenName: req.body.givenName,
familyName: req.body.familyName
},
email: req.body.email
};
}
困难在于验证总是失败。
我的app.j包含以下用法语句:
// all environments
app.set('port', process.env.PORT || 3000);
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.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//
//Routes
app.get('/', routes.index);
app.post('/signup', users.signup);
出于想法......似乎req.body.xxx没有绑定......
感谢任何降低我无知程度的指导。
干杯