[nodejs] nodejs开发个人博客(五)分配数据

前端之家收集整理的这篇文章主要介绍了[nodejs] nodejs开发个人博客(五)分配数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用回掉大坑进行取数据

 能看明白的就看,看不明白的手动滑稽

首页控制器 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><span style="color: #0000ff;"&gt;var</span> categoryModel=F.model("category"<span style="color: #000000;"&gt;);
</span><span style="color: #0000ff;"&gt;var</span> articleModel=F.model("article"<span style="color: #000000;"&gt;);
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <a href="https://www.jb51.cc/tag/fenlei/" target="_blank" class="keywords">分类</a>数据</span>
categoryModel.getAllList(<span style="color: #0000ff;"&gt;function</span><span style="color: #000000;"&gt;(err,categoryList){
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a>条数</span>
    articleModel.getCount(cid,<span style="color: #0000ff;"&gt;function</span><span style="color: #000000;"&gt;(err,nums){
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <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;"&gt;var</span> nextPage=(currentPage+1)>=Math.ceil(nums[0].num/pageSize) ? Math.ceil(nums[0].num/pageSize) : currentPage+1<span style="color: #000000;"&gt;;
            </span><span style="color: #0000ff;"&gt;var</span> prePage=(currentPage-1)<=0 ? 1 : currentPage-1<span style="color: #000000;"&gt;;
            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 归档</span>
            articleModel.getArchives(<span style="color: #0000ff;"&gt;function</span><span style="color: #000000;"&gt;(err,allArticleTime){
                </span><span style="color: #0000ff;"&gt;var</span> newArticleTime=<span style="color: #000000;"&gt;[];
                </span><span style="color: #0000ff;"&gt;for</span>(<span style="color: #0000ff;"&gt;var</span> i=0;i<allArticleTime.length;i++<span style="color: #000000;"&gt;){
                    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;"&gt;,allArticleTime[i].time));
                }
                </span><span style="color: #008000;"&gt;/*</span><span style="color: #008000;"&gt;分配数据</span><span style="color: #008000;"&gt;*/</span>
                <span style="color: #0000ff;"&gt;var</span> data=<span style="color: #000000;"&gt;{
                    categoryList:categoryList,articleList:articleList,cid:cid,nextPage:nextPage</span>==0 ? 1<span style="color: #000000;"&gt; : nextPage,prePage:prePage,allArticleTime:newArticleTime,currentPage:currentPage
                };

                </span><span style="color: #008000;"&gt;/*</span><span style="color: #008000;"&gt;渲染模板</span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt;
                res.render(</span>"home/index"<span style="color: #000000;"&gt;,data);    
            });            
        });
    });

});

</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;F.model("category").addCate({"name":"测试"});</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;F.model("category").saveCate({"name":"测试1"},"id=4");</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;F.model("category").delCate("id=4");</span>
<span style="color: #008000;"&gt;/*</span><span style="color: #008000;"&gt;渲染模板</span><span style="color: #008000;"&gt;*/</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;res.render("home/index");</span>

<span style="color: #000000;">});
module.exports=router;

文章模型:

文章模型文件 =获取条数 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

猜你在找的Node.js相关文章