前言
前台内容的展示,是将博客的文章内容从数据库中查询出来,再将数据绑定给模板引擎,由模板引擎渲染出来。由于博客首页的数据是需要分页展示的所以就需要调用之前自己定义的的博客分页处理模块 /my_modules/pagination.js ,但在此之前得先将博客的文章分类信息从数据库中查询出来,在查询博客分类信息的回调函数中来调用 pagination.js 渲染内容。
首页路由修改
由前端使用 GET 方式向后端传递用户当前点击的文章分类的 ID ,后端根据文章分类 ID 的不同而从数据库中查询出相应的博客内容。
修改首页的路由处理文件 /router/main/main.js ,修改为如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| const express = require("express"); const categoryModel = require("../models/category"); const contentModel = require("../models/content");
const pagination = require("../my_modules/pagination");
const router = express.Router();
router.get("/", (req, res) => { let other = {}; let where = {}; if (req.query.categoryId) { other.categoryId = req.query.categoryId; where.category = req.query.categoryId; }
categoryModel.find({}, (err, categories) => { if (!err) { other.categories = categories; pagination({ limit: 10, model: contentModel, url: "/", ejs: "main/index", where: where, res: res, req: req, populate: ["category", "author"], other: other }); } else { throw err; } }); });
module.exports = router;
|
博客首页模板视图的修改
修改 /views/main/index.ejs 为如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="/public/css/bootstrap.min.css"> <title>TFLIN'BLOG</title> </head> <body> <header> <header> <nav class="navbar navbar-default"> <div class="navbar-header"> <a class="navbar-brand" href="/">TFL'BLOG</a> </div> <div class="container"> <ul class="nav navbar-nav"> <% if (!other.categoryId) {%> <li class="active"><a href="/">首页</a></li> <% } else {%> <li><a href="/">首页</a></li> <% } %> <% for (let i = 0, len = other.categories.length; i < len; i++) {%> <% if (other.categoryId === other.categories[i].id) {%> <li class="active"><a href="/?categoryId=<%= other.categories[i].id %>"><%= other.categories[i].name %></a></li> <% } else {%> <li><a href="/?categoryId=<%= other.categories[i].id %>"><%= other.categories[i].name %></a></li> <% } %> <% } %> </ul> </div> </nav> </header> </header> <div class="container"> <div class="col-md-8"> <% for(let i = 0, len = docs.length; i < len; i++) { %> <div class="well"> <h3><%=docs[i].title%></h3> <p><%=docs[i].description%></p> <a href="javascript:;" class="btn btn-info">阅读全文</a> <span class="text-info pull-right">阅读量:<%=docs[i].views%> | 时间:<%=docs[i].addTime%> | 作者:<%=docs[i].author.username%> | 分类:<%=docs[i].category.name%></span> </div> <% } %> <%- include("../pagination") %> </div> <div class="col-md-4"> <%if (!userInfo.userid) {%> <div id="login"> <h2>登录</h2> <div class="input-group"> <span class="input-group-addon">用户</span> <input type="text" class="form-control" name="username" placeholder="请输入用户名"> </div> <br> <div class="input-group"> <span class="input-group-addon">密码</span> <input type="password" class="form-control" name="password" placeholder="请输入密码"> </div> <br> <button type="button" name="button" class="btn btn-primary form-control" id="login-btn">登录</button> <br><br> <a href="javascripts:;" class="col-md-offset-4">没有账号?点击注册</a> <div class="alert alert-success alert-dismissable hide" role="alert"> <button class="close" type="button">×</button> <span>恭喜您操作成功!</span> </div> </div> <div id="reg" class="hide"> <h2>注册</h2> <div class="input-group"> <span class="input-group-addon">用户名称</span> <input type="text" class="form-control" name="username" placeholder="请输入用户名"> </div> <br> <div class="input-group"> <span class="input-group-addon">输入密码</span> <input type="password" class="form-control" name="password" placeholder="请输入密码"> </div> <br> <div class="input-group"> <span class="input-group-addon">确认密码</span> <input type="password" class="form-control" name="repassword" placeholder="请再次输入密码"> </div> <br> <button type="button" name="button" class="btn btn-primary form-control" id="reg-btn">注册</button> <br><br> <a href="javascripts:;" class="col-md-offset-4">已有账号?点击登录</a> <div class="alert alert-success alert-dismissable hide" role="alert"> <button class="close" type="button">×</button> <span>恭喜您操作成功!</span> </div> </div> <%} else {%> <div id="user-info"> <div class="panel panel-primary"> <div class="panel-heading">用户面板</div> <div class="panel-body"> <%if (userInfo.isadmin) {%> <h3>欢迎尊贵的管理员,<%=userInfo.username%>!</h3> <p> <a href="/admin" class="btn btn-primary form-control">进入控制台</a> </p> <%} else {%> <h3>欢迎您,<%=userInfo.username%>!</h3> <%}%> <p> <button type="button" name="button" id="logout" class="btn btn-danger form-control">注销</button> </p> </div> </div> </div> <%}%> </div> </div> <script src="/public/js/3.3.1-jquery-min.js"></script> <script src="/public/js/bootstrap.min.js"></script> <script src="/public/js/main.js"></script> </body> </html>
|
测试
在管理员控制台,多添加几篇不同分类的文章,然后返回博客首页进行测试