这篇文章主要介绍nodejs个人博客开发之如何实现分配数据,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
使用回掉大坑进行取数据
能看明白的就看,看不明白的手动滑稽
var router=express.Router();
var pageSize=5;
router.get('/',function(req,res,next){
var currentPage=parseInt(req.params.page);
var cid=0;
var categoryModel=F.model("category");
var articleModel=F.model("article");
// 分类数据
categoryModel.getAllList(function(err,categoryList){
// 文章条数
articleModel.getCount(cid,function(err,nums){
// 文章分页
articleModel.getArticlePager(cid,currentPage,pageSize,function(err,articleList){
var nextPage=(currentPage+1)>=Math.ceil(nums[0].num/pageSize) ? Math.ceil(nums[0].num/pageSize) : currentPage+1;
var prePage=(currentPage-1)<=0 ? 1 : currentPage-1;
// 归档
articleModel.getArchives(function(err,allArticleTime){
var newArticleTime=[];
for(var i=0;i<allArticleTime.length;i++){
newArticleTime.push(F.phpDate("y年m月",allArticleTime[i].time));
}
var data={
categoryList:categoryList,
articleList:articleList,
cid:cid,
nextPage:nextPage==0 ? 1 : nextPage,
prePage:prePage,
allArticleTime:newArticleTime,
currentPage:currentPage
};
res.render("home/index",data);
});
});
});
});
//F.model("category").addCate({"name":"测试"});
//F.model("category").saveCate({"name":"测试1"},"id=4");
//F.model("category").delCate("id=4");
//res.render("home/index");
});
module.exports=router;
文章模型:
module.exports={
getCount:function(categoryId,callback){
var condition="";
if(categoryId!=0){
condition="where category_id="+categoryId;
}
var sql="select count(*) num from article "+condition;
db.query(sql,callback);
},
getArticlePager:function(categoryId,currentPage,pageSize,callback){
if(currentPage<=0||!currentPage) currentPage=1;
var start=(currentPage-1)*pageSize;
var end=pageSize;
var condition="";
if(categoryId!=0){
condition="where category_id="+categoryId;
}
var sql="select * from article "+condition+" order by time desc limit "+start+","+end;
db.query(sql,callback);
},
getArchives:function(callback){
db.query("select time from article order by time desc",callback);
}
};
以上是“nodejs个人博客开发之如何实现分配数据”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!