前臺(tái)用的jade模板,這里用了bootstrap布局和layui的分頁控件。
index.jade main code:
//文章列表顯示 p.container if(articals.length > 0) p.container.clear - for(var i=0;i<articals.length;i++) .col-lg-6.col-md-6.col-sm-12 p.panel.panel-default .panel-heading a.block.bold(href = '/details?id=#{articals[i]._id}')= articals[i].title p.panel-body.height50!= articals[i].detail p.panel-footer span.glyphicon.glyphicon-user(aria-hidden = "true") span(style = "margin-right:20px;margin-left:5px;")= articals[i].author span= articals[i].createDate //添加分頁 p#pager.text-center.col-lg-12 col-md-12 col-sm-12 .empty .empty script(type="text/javascript"). layui.use(['laypage', 'layer'], function(){ var laypage = layui.laypage; laypage.render({ elem: 'pager' ,count: #{total} ,curr:('#{page}'||1) ,jump: function(obj, first){ if(!first){ window.location.href = '/index/'+obj.curr; } } }); }); else h2 快去發(fā)表一篇文章吧~ #footer .container.text-center p 版權(quán)所有 © 2017 LorettaXiongField
index.js main code:
var express = require('express'); var mongoose = require('mongoose');require('../models/schema');var article = mongoose.model('Article');var user = mongoose.model('User');var url = require('url');var router = express.Router(); //處理get請(qǐng)求router.get('/', function(req, res, next) { var con = []; article.find({}).exec(function(err, doc){ var total = doc.length; //按時(shí)間順序查找前10條文章的數(shù)據(jù) article.find({}).sort({createDate: -1}).populate('creator', 'name').limit(10).exec(function(err, character){ //添加容錯(cuò)措施 if(err){ console.log(err); }else if(character.length == 0){ //沒有任何文章,就直接返回 console.log('no articles!'); res.render('index', { title: '首頁', articals : con }); }else{ for(var i = 0; i < character.length; i++){ con[i] = { _id: character[i]._id, title: character[i].title, detail: character[i].text.replace(/<[\/]{0,1}[a-zA-Z\s"'=;]{1,}>/g, ''), author: character[i].creator.name, createDate: character[i].createDate }; } //返回文章總數(shù)以及前10條數(shù)據(jù) res.render('index', { title: '首頁', total: total, articals : con }); } }); }) }); //處理帶有頁碼參數(shù)的get請(qǐng)求router.get('/index/:page', function(req, res, next) { var con = []; var pageNum = parseInt(req.params.page); var Creator; if(pageNum){ article.find({}).exec(function(err, doc){ var total = doc.length; //按時(shí)間順序查找從第[(頁碼-1)*10]條數(shù)據(jù)開始的10條文章的數(shù)據(jù) article.find({}).sort({createDate: -1}).populate('creator', 'name').skip((pageNum-1)*10).limit(10).exec(function(err, character){ //添加容錯(cuò)措施 if(err){ console.log(err); }else if(character.length == 0){ console.log('no articles!'); res.render('index', { title: '首頁', articals : con }); }else{ for(var i = 0; i < character.length; i++){ con[i] = { _id: character[i]._id, title: character[i].title, detail: character[i].text.replace(/<[\/]{0,1}[a-zA-Z\s"'=;]{1,}>/g, ''), author: character[i].creator.name, createDate: character[i].createDate }; } //返回新的頁碼以及相應(yīng)的10條數(shù)據(jù) res.render('index', { title: '首頁', total: total, page: pageNum, articals : con }); } }); }); } }); module.exports = router;
下面附加app.js的主要代碼:// 模塊依賴var express = require('express');
var path = require('path'); var logger = require('morgan'); var http = require('http'); var favicon = require('serve-favicon'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); require('./models/schema'); var user = mongoose.model('User'); var cookieParser = require('cookie-parser'); var session = require('express-session'); var app = express(); //日志設(shè)置app.use(logger('dev')); //圖標(biāo)設(shè)置app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); //視圖設(shè)置app.set('view engine', 'jade'); app.set('views', path.join(__dirname, 'views')); //靜態(tài)文件設(shè)置app.use(express.static(path.join(__dirname, 'public'))); //表單數(shù)據(jù)解析app.use(bodyParser.json()); app.use(bodyParser.urlencoded( { extended: true } )); //路由設(shè)置var routes = require('./routes/index'); app.use('/', routes); //服務(wù)器設(shè)置var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log('server start on 127.0.0.1:8081'); } )
下面是本人實(shí)現(xiàn)的效果:
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com