<fieldset> <legend>用户注册</legend> <div class="control-group"> <label class="control-label" for="username">用户名</label> <div class="controls"> <input type="text" class="input-xlarge" id="username" name="username"> <p class="help-block">你的账户名称,用于登录且必须填写。</p> </div> </div> <div class="control-group"> <label class="control-label" for="nickName">昵称</label> <div class="controls"> <input type="text" class="input-xlarge" id="nickName" name="nickName"> <p class="help-block">你的昵称,用于显示。若不填写昵称,则默认显示用户名。</p> </div> </div> <div class="control-group"> <label class="control-label" for="password">口令</label> <div class="controls"> <input type="password" class="input-xlarge" id="password" name="password"> </div> </div> <div class="control-group"> <label class="control-label" for="passwordRepeat">重复输入口令</label> <div class="controls"> <input type="password" class="input-xlarge" id="passwordRepeat" name="passwordRepeat"> </div> </div> <div class="control-group"> <label class="control-label" for="email">邮箱</label> <div class="controls"> <input type="text" class="input-xlarge" id="email" name="email"> </div> </div> <div class="control-group"> <label class="control-label" for="userImg">自定义用户头像</label> <div class="controls"> <input type="file" class="input-xlarge" id="userImg" name="userImg"> <p class="help-block">暂只支持png、jpeg格式</p> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">注册</button> </div> </fieldset>
/*点击‘注册’按钮提交用户信息*/ router.post('/doReg', function(req, res) { console.log(JSON.stringify(req.body)); //用户名是否为空 var username = Util.trim(req.body.username); if(username == '') { //用户名为空! return res.redirect('/reg?reg_msg=0'); } //两次输入的口令是否一致 //用户口令 var password = req.body.password; //重复口令 var passwordRepeat = req.body.passwordRepeat; if(password != passwordRepeat) { //用户两次输入的口令不一致! return res.redirect('/reg?reg_msg=1'); } //生成口令的散列值 var md5 = crypto.createHash('md5'); password = md5.update(password).digest('base64'); //昵称 var nickName = Util.trim(req.body.nickName); if(nickName == '') { nickName = username; } //邮箱 var email = Util.trim(req.body.email); if(email != '') { if(!Util.chkFormatForEmail(email)) { //邮箱格式不正确! return res.redirect('/reg?reg_msg=2'); } } //-------------------------自定义头像处理------------------------------------- var fileObj = req.files.userImg; var newFileName = "/myall/img/users/default.png"; //若添加用户自定义头像且图片格式为image/png、image/jpeg if(fileObj.name != '') { if("image/png,image/jpeg".indexOf(fileObj.type) == -1) { //上传图片格式不正确! return res.redirect('/reg?reg_msg=4'); } /* //文件大小1M if(fileObj.size > 1048576) { //上传图片太大! return res.redirect('/reg?reg_msg=5'); } */ newFileName = "/myall/img/users/" + username + fileObj.name.substr(fileObj.name.indexOf(".")); console.log("newFileName==="+newFileName); fs.rename(fileObj.path, "./public"+newFileName, function(err) { if(err){ console.log(err); //注册过程中出错! return res.redirect('/reg?reg_msg=6'); } }); } var today = Util.getToday('yyyy-MM-dd HH:mm:ss'); var newUser = new User({ userName: username, password: password, nickName: nickName, headUrl: newFileName, regTime: today, loginTime: today, lastLogoutTime: '', isOnLine: true, email: email, funcsCount: 0 }); //判断注册用户是否存在 User.isExist(username, function(err, user) { if(err) { console.log(err); //注册过程中出错! return res.redirect('/reg?reg_msg=error'); } if(user) { //用户名已经被注册! return res.redirect('/reg?reg_msg=3'); } newUser.save(function(err) { if(err) { console.log(err); //注册过程中出错! return res.redirect('/reg?reg_msg=error'); } req.session.user = newUser; res.redirect('/regSuc'); }); }); //-------------------------------------------------------------- }); /*跳转至用户注册成功自动跳转页面*/ router.get('/regSuc', function(req, res) { res.render('./content/reg_success', { title: '注册成功', user: req.session.user}); });
<div class="hero-unit"> <h3> <%= user.nickName %>,您好!</h3> <p>恭喜您已成功注册,该页面将在<span id="jumpTo" style="color:red;">10</span>秒之后会自动跳转到首页。</p> <script type="text/javascript"> function countDown(secs, surl) { document.getElementById('jumpTo').innerHTML = secs; if(secs -- > 0) { setTimeout("countDown("+secs+", '"+surl+"')", 1100); } else { location.href = surl; } } countDown(document.getElementById('jumpTo').innerHTML, "/"); </script> <p> 若无法正常跳转,<a class="btn btn-primary btn-large" href="/">请点击这里</a> </p> </div>
router.get('/', function(req, res) { Question.query({}, function (err, docs) { if(err) { console.log(err); } res.render('index', { title: '首页', toUrl: '/', items: docs, user: req.session.user}); }); });
以上仅作为备忘或分享,若有错误请大家指正或讨论!