NodeJS实战 利用Express&MongoDB搭建博客(7)内容显示

分类的展示

    /routers/main.js

var express = require('express');
var router = express.Router();
var Category = require('../models/Category');
router.get('/',function (req,res,next) {
    //读取所有分类
    Category.find().then(function (categories) {
        //console.log(rs);
        res.render('main/index',{
            userInfo:req.userInfo,
            categories:categories
        });
    });
});

module.exports = router;

    /views/main/layout.html

<nav>
    <div class="menu">
        {%if category == ''%}
        <a href="/" class="focus">首页</a>
        {%else%}
        <a href="/">首页</a>
        {%endif%}

        {%for cate in categories%}
        {%if category == cate.id%}
        <a href="/?category={{cate.id}}" class="focus">{{cate.name}}</a>
        {%else%}
        <a href="/?category={{cate.id}}">{{cate.name}}</a>
        {%endif%}
        {%endfor%}
    </div>
</nav>

 内容首页

    /views/admin/layout.html

{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
    <li><a href="/">管理首页</a></li>
    <li><span>内容列表</span></li>
</ol>
<h3>内容列表</h3>

<table class="table table-hover table-striped">
    <tr>
        <th>ID</th>
        <th>分类名称</th>
        <th>操作</th>
    </tr>
    <!--{% for content in contents %}
    <tr>
        <td>{{content._id.toString()}}</td>
        <td>{{content.name}}</td>
        <td>
            <a href="/admin/category/edit?id={{category._id.toString()}}">修改</a>
            <a href="/admin/category/delete?id={{category._id.toString()}}">删除</a>
        </td>
    </tr>
    {% endfor %}-->
</table>
{%include 'page.html'%}
{% endblock %}

    /routers/admin.js

/**
 * 内容首页
 */
router.get('/content',function (req,res) {
    res.render('admin/content_index',{
        userInfo:req.userInfo
    });
});

内容添加及保存

    /schemas/contents.js

var mongoose = require('mongoose');

//内容数据结构
module.exports = new mongoose.Schema({
    //关联字段-分类的id
    category:{
        type:mongoose.Schema.Types.ObjectId,
        //引用
        ref:'Content'
    },
    //内容标题
    title:String,
    //内容简介
    description:{
        type: String,
        default:''
    },
    //内容
    content:{
        type:String,
        default: ''
    }
});

    /models/Contents.js

var mongoose = require('mongoose');
var contentsSchema = require('../schemas/contents');

module.exports = mongoose.model('Content',contentsSchema);

    /routers/admin.js

var Content = require('../models/Content');
/.../
/**
 * 内容添加
 */
router.get('/content/add',function (req,res) {
    Category.find().sort({_id:-1}).then(function (categories) {
        res.render('admin/content_add',{
            userInfo:req.userInfo,
            categories:categories
        });
    });
});
/**
 * 内容保存
 */
router.post('/content/add',function (req,res) {
    if(req.body.category==''){
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'分类不能为空'
        });
        return;
    }
    if(req.body.title==''){
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'标题不能为空'
        });
        return;
    }
    new Content({
        category: req.body.category,
        title:req.body.title,
        description:req.body.description,
        content:req.body.content
    }).save().then(function (rs) {
        res.render('admin/success',{
            userInfo:req.userInfo,
            message:'内容保存成功',
            url:'/admin/content'
        });
    });

});

    /views/admin/content_add.html

{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
    <li><a href="/">管理首页</a></li>
    <li><span>内容添加</span></li>
</ol>
<h3>内容添加</h3>
<form role="form" method="post">
    <div class="form-group">
        <label for="title">分类:</label>
        <select name="category" id="category" class="form-control">
            {%for category in categories%}
            <option value="{{category.id}}">{{category.name}}</option>
            {%endfor%}
        </select>
    </div>
    <div class="form-group">
        <label for="title">标题:</label>
        <input type="text" class="form-control" id="title" placeholder="请输入内容标题" name="title">
    </div>
    <div class="form-group">
        <label for="description">简介:</label>
        <textarea name="description" id="description" class="form-control" rows="5" placeholder="请输入内容简介"></textarea>
    </div>
    <div class="form-group">
        <label for="content">内容:</label>
        <textarea name="content" id="content" class="form-control" rows="10" placeholder="请输入内容"></textarea>
    </div>
    <button type="submit" class="btn btn-default">提交</button>
</form>
{% endblock %}

内容显示

    /routers/admin.js

/**
 * 内容首页
 */
router.get('/content',function (req,res) {
    var page = Number(req.query.page || 1);
    var limit = 10;
    var pages = 0;

    Content.countDocuments().then(function(count) {

        //计算总页数
        pages = Math.ceil(count / limit);
        //取值不能超过pages
        page = Math.min( page, pages );
        //取值不能小于1
        page = Math.max( page, 1 );

        var skip = (page - 1) * limit;

        Content.find().limit(limit).skip(skip).populate('category').then(function(contents) {
            
            res.render('admin/content_index', {
                userInfo: req.userInfo,
                contents: contents,

                count: count,
                pages: pages,
                limit: limit,
                page: page
            });
        });

    });
});

    /view/admin/content_index.html

{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
    <li><a href="/">管理首页</a></li>
    <li><span>内容列表</span></li>
</ol>
<h3>内容列表</h3>

<table class="table table-hover table-striped">
    <tr>
        <th>ID</th>
        <th>分类名称</th>
        <th>标题</th>
        <th>操作</th>
    </tr>
    {% for content in contents %}
    <tr>
        <td>{{content._id.toString()}}</td>
        <td>{{content.category.name}}</td>
        <td>{{content.title}}</td>
        <td>
            <a href="/admin/content/edit?id={{content._id.toString()}}">修改</a>
            <a href="/admin/content/delete?id={{content._id.toString()}}">删除</a>
        </td>
    </tr>
    {% endfor %}
</table>
{%include 'page.html'%}
{% endblock %}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值