提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。现在我们需要允许我们网站上的新用户注册一个帐户。在主页的res.render上,向传递的对象添加一个新变量 - showRegistration: true
。刷新页面时,您应该会看到已在index.pug文件中创建的注册表单!此表单设置为POST on / register,因此我们应该设置此接受POST并在数据库中创建用户对象。注册路由的逻辑应如下所示:注册新用户>验证新用户>重定向到/配置文件步骤1的逻辑,注册新用户,应如下所示:使用findOne命令查询数据库>如果用户返回然后它存在并重定向回到主页或者如果用户未定义且没有发生错误,则使用用户名和密码将“insertOne”输入数据库,只要没有错误发生,然后调用next转到步骤2,验证新的user,我们已经在POST / login路由中编写了逻辑。 app.route( '/寄存器')
.post((req,res,next)=> {
db.collection('users')。findOne({username:req.body.username},function(err,user){
if(错误){
下一个(ERR);
} else if(user){
res.redirect( '/');
} else {
db.collection( '用户')。insertOne(
{username:req.body.username,
密码:req.body.password},
(错误,doc)=> {
if(错误){
res.redirect( '/');
} else {
next(null,user);
}
}
)
}
})},
passport.authenticate('local',{failureRedirect:'/'}),
(req,res,next)=> {
res.redirect( '/简档');
}
);
当您认为自己已经做对时,请提交您的页面。如果您遇到错误,可以在这里查看到目前为止完成的项目。
## Instructions