使用回掉大坑进行取数据
能看明白的就看,看不明白的手动滑稽
首页控制器
router=
pageSize=5router.get('/',<span style="color: #0000ff;">function<span style="color: #000000;">(req,res,next){
<span style="color: #0000ff;">var currentPage=<span style="color: #000000;">parseInt(req.params.page);
<span style="color: #0000ff;">var cid=0<span style="color: #000000;">;
<span style="color: #0000ff;">var currentPage=<span style="color: #000000;">parseInt(req.params.page);
<span style="color: #0000ff;">var cid=0<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> categoryModel=F.model("category"<span style="color: #000000;">);
</span><span style="color: #0000ff;">var</span> articleModel=F.model("article"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/fenlei/" target="_blank" class="keywords">分类</a>数据</span>
categoryModel.getAllList(<span style="color: #0000ff;">function</span><span style="color: #000000;">(err,categoryList){
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a>条数</span>
articleModel.getCount(cid,<span style="color: #0000ff;">function</span><span style="color: #000000;">(err,nums){
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a><a href="https://www.jb51.cc/tag/fenye/" target="_blank" class="keywords">分页</a></span>
articleModel.getArticlePager(cid,currentPage,pageSize,articleList){
</span><span style="color: #0000ff;">var</span> nextPage=(currentPage+1)>=Math.ceil(nums[0].num/pageSize) ? Math.ceil(nums[0].num/pageSize) : currentPage+1<span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> prePage=(currentPage-1)<=0 ? 1 : currentPage-1<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 归档</span>
articleModel.getArchives(<span style="color: #0000ff;">function</span><span style="color: #000000;">(err,allArticleTime){
</span><span style="color: #0000ff;">var</span> newArticleTime=<span style="color: #000000;">[];
</span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> i=0;i<allArticleTime.length;i++<span style="color: #000000;">){
newArticleTime.push(F.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Date(</span>"y年m月"<span style="color: #000000;">,allArticleTime[i].time));
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;">分配数据</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">var</span> data=<span style="color: #000000;">{
categoryList:categoryList,articleList:articleList,cid:cid,nextPage:nextPage</span>==0 ? 1<span style="color: #000000;"> : nextPage,prePage:prePage,allArticleTime:newArticleTime,currentPage:currentPage
};
</span><span style="color: #008000;">/*</span><span style="color: #008000;">渲染模板</span><span style="color: #008000;">*/</span><span style="color: #000000;">
res.render(</span>"home/index"<span style="color: #000000;">,data);
});
});
});
});
</span><span style="color: #008000;">//</span><span style="color: #008000;">F.model("category").addCate({"name":"测试"});</span>
<span style="color: #008000;">//</span><span style="color: #008000;">F.model("category").saveCate({"name":"测试1"},"id=4");</span>
<span style="color: #008000;">//</span><span style="color: #008000;">F.model("category").delCate("id=4");</span>
<span style="color: #008000;">/*</span><span style="color: #008000;">渲染模板</span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;">res.render("home/index");</span>
<span style="color: #000000;">});
module.exports
文章模型:
文章模型文件
=获取条数 condition=""(categoryId!=0="where category_id="+ sql="select count(*) num from article "+sql,callback);
},获取分页数据(currentPage<=0||!currentPage) currentPage=1 start=(currentPage-1)* end= condition=""(categoryId!=0="where category_id="+ sql="select * from article "+condition+" order by time desc limit "+start+","+sql,"select time from article order by time desc"
原文链接:https://www.f2er.com/nodejs/69295.html