说明:仿node.js 开发指南的一个微博系统
1.github 地址 https://github.com/wblt/Node--Microblog
2.部分代码
varexpress=require('express');
varrouter=express.Router();
varcrypto=require('crypto');
varUser=require('../models/user.js');
varPost=require('../models/post.js');
/* GET home page. */
//router.get('/', function(req, res) {
// res.render('index', { title: 'Express' });
//});
router.get('/',function(req, res) {
Post.get(null,function(err,posts){
if(err){
posts = [];
}
res.render('index',{
title:'首页',
posts:posts
});
});
});
router.get("/u/:user",function(req,res){
User.get(req.params.user,function(err,user){
if(!user){
req.flash('error','用户不存在');
returnres.redirect('/');
}
Post.get(user.name,function(err,posts){
if(err){
req.flash('error',err);
returnres.redirect('/');
}
res.render('user',{
title:user.name,
posts:posts
});
});
});
});
router.post('/post',checkLogin);
router.post("/post",function(req,res){
varcurrentUser= req.session.user;
varpost=newPost(currentUser.name,req.body.post);
post.save(function(err){
if(err){
req.flash('error',err);
returnres.redirect('/');
}
req.flash('success','发表成功');
console.log(currentUser);
res.redirect('/u/'+currentUser.name);
});
});
router.get('/reg',checkNotLogin);
router.get('/reg',function(req,res){
res.render('reg',{
title:"用户注册"
});
});
router.post('/reg',checkNotLogin);
router.post("/reg",function(req,res){
console.log(req.body);
//检验用户两次输入口令是否一致
if(req.body['password-repeat']!=req.body['password']){
req.flash('error','两次输入的口令不一致');
returnres.redirect('/reg');
}
//生成口令的散列值
varmd5=crypto.createHash('md5');
varpassword=md5.update(req.body.password).digest('base64');
varnewUser=newUser({
name: req.body.username,
password:password
});
//检查用户名是否已经存在
User.get(newUser.name,function(err,user){
if(user){
err ='Username already exists.';
}
if(err){
req.flash('error',err);
console.log("err");
returnres.redirect('/reg');
}
console.log("save");
//如果不存在则新增用户
newUser.save(function(err){
if(err){
req.flash('error',err);
console.log("save err");
console.log(err);
returnres.redirect('/reg');
}
req.session.user=newUser;
req.flash('success','注册成功');
returnres.redirect('/');
});
});
});
router.get('/login',checkNotLogin);
router.get("/login",function(req,res){
res.render('login',{
title:'用户登入'
});
});
router.post('/login',checkNotLogin);
router.post("/login",function(req,res){
//生成口令的散列值
varmd5=crypto.createHash('md5');
varpassword=md5.update(req.body.password).digest('base64');
User.get(req.body.username,function(err,user){
if(!user){
req.flash('error','用户不存在');
returnres.redirect('/login');
}
if(user.password!=password){
req.flash('error','用户口令错误');
returnres.redirect('/login');
}
req.session.user= user;
req.flash('success','登入成功');
res.redirect('/');
});
});
router.get("/logout",checkLogin);
router.get("/logout",function(req,res){
req.session.user=null;
req.flash('success','登出成功');
res.redirect('/');
});
functioncheckLogin(req,res,next){
if(!req.session.user){
req.flash('error',"未登入");
returnres.redirect('/login');
}
next();
};
functioncheckNotLogin(req,res,next){
if(req.session.user){
req.flash("error","已登入");
returnres.redirect('/');
}
next();
};
module.exports=router;
3.效果图
网友评论