nodejs個人博客開發(fā)第五步 分配數(shù)據(jù)
更新時間:2017年04月12日 12:00:09 作者:陶士涵
這篇文章主要為大家詳細介紹了nodejs個人博客開發(fā)的分配數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了nodejs個人博客開發(fā)的分配數(shù)據(jù),具體內(nèi)容如下
使用回掉大坑進行取數(shù)據(jù)
能看明白的就看,看不明白的手動滑稽
/**
* 首頁控制器
*/
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);
}
};
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 從零學習node.js之利用express搭建簡易論壇(七)
- node.js博客項目開發(fā)手記
- Node.js+jade抓取博客所有文章生成靜態(tài)html文件的實例
- 利用Vue.js+Node.js+MongoDB實現(xiàn)一個博客系統(tǒng)(附源碼)
- nodejs個人博客開發(fā)第七步?后臺登陸
- nodejs個人博客開發(fā)第六步 數(shù)據(jù)分頁
- nodejs個人博客開發(fā)第四步 數(shù)據(jù)模型
- nodejs個人博客開發(fā)第三步 載入頁面
- nodejs個人博客開發(fā)第二步 入口文件
- nodejs個人博客開發(fā)第一步 準備工作
- node.js實現(xiàn)博客小爬蟲的實例代碼
- [將免費進行到底]在Amazon的一年免費服務器上安裝Node.JS, NPM和OurJS博客
- node+koa2+mysql+bootstrap搭建一個前端論壇
相關文章
Node.js如何實現(xiàn)文件夾內(nèi)文件批量重命名
這篇文章主要為大家詳細介紹了Node.js如何實現(xiàn)文件夾內(nèi)文件批量重命名功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2025-03-03

