溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

nodejs個(gè)人博客開發(fā)之如何實(shí)現(xiàn)分配數(shù)據(jù)

發(fā)布時(shí)間:2021-08-20 11:34:33 來源:億速云 閱讀:109 作者:小新 欄目:web開發(fā)

這篇文章主要介紹nodejs個(gè)人博客開發(fā)之如何實(shí)現(xiàn)分配數(shù)據(jù),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

使用回掉大坑進(jìn)行取數(shù)據(jù)

能看明白的就看,看不明白的手動(dòng)滑稽

/**
* 首頁控制器
*/
var router=express.Router();
/*每頁條數(shù)*/
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");
  // 分類數(shù)據(jù)
  categoryModel.getAllList(function(err,categoryList){
    // 文章條數(shù)
    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));
          }
          /*分配數(shù)據(jù)*/
          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={
  /*獲取條數(shù)*/
  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);
  },
  /*獲取分頁數(shù)據(jù)*/
  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個(gè)人博客開發(fā)之如何實(shí)現(xiàn)分配數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI