首页 > 代码库 > 夺命雷公狗---node.js---21之项目的构建在node+express+mongo的博客项目6之数据的遍历

夺命雷公狗---node.js---21之项目的构建在node+express+mongo的博客项目6之数据的遍历

首先还是来链接数据库,然后就查找,如下所示:

技术分享

 

 

 

/** * Created by leigood on 2016/8/31. */var express = require(‘express‘);var router = express.Router();//引入mongodb模块var MongoClient = require(‘mongodb‘).MongoClient;var DB_STR = "mongodb://localhost:27017/blog";//这里的是在mongodb下建立的库var ObjectId = require(‘mongodb‘).ObjectId;  //这里主要是用来处理mongodb下id的/* GET users listing. */router.get(‘/‘, function(req, res, next) {    MongoClient.connect(DB_STR,function(err,db){        if(err){            throw err;            return;        }        var c = db.collection(‘category‘);        c.find().toArray(function(err,desc){            if(err){                res.send(err);            }            res.render(‘Admin/category‘,{data:desc});        });    });});router.get(‘/add‘,function(req,res,next){    res.render(‘Admin/category_add‘);});router.post(‘/add‘,function(req,res){    var title = req.body.title;    var sort = req.body.sort;    //console.log(title+‘-------‘+sort);    MongoClient.connect(DB_STR,function(err,db){        if(err){            throw err;            return;        }        //此处的db,就是blog数据库        var c = db.collection(‘category‘);        c.insert({title:title,sort:sort},function(err,result){            if(err){                err.send(err);            }else{                res.send(‘添加分类成功 <a href="http://www.mamicode.com/Admin/category">查看列表</a>‘);            }        });    });});router.get(‘/edit‘,function(req,res,next){    res.render(‘Admin/category_edit‘);});module.exports = router;

 

 

然后就开始将数据遍历到前台模版上去即可。。。

 

技术分享

 

 

<% include header.html %><% include left.html %>            <!-- Right side column. Contains the navbar and content of the page -->            <aside class="right-side">                <!-- Content Header (Page header) -->                <section class="content-header">                    <h1>                        分类列表                        <small>分类</small>                    </h1>                    <ol class="breadcrumb">                        <li><a href="index.html"><i class="fa fa-dashboard"></i> 管理中心</a></li>                        <li class="category_list.html"><a href="index.html">分类</a></li>                        <li class="active">分类列表</li>                    </ol>                </section>                <!-- Main content -->                <section class="content">                    <div class="row">                        <div class="col-md-12">                            <div class="box">                                <div class="box-header">                                    <h3 class="box-title"></h3>                                    <a href="/admin/category/add" class="btn btn-default pull-right">添加分类</a>                                </div><!-- /.box-header -->                                <div class="box-body">                                    <table class="table table-bordered">                                        <tbody><tr>                                            <th style="width: 10px">#</th>                                            <th>分类</th>                                            <th>文章数量</th>                                            <th>排序</th>                                            <th style="width: 20%">操作</th>                                        </tr>                                        <% data.forEach(function(item,idx){ %>                                        <tr>                                            <td><%= idx+1 %></td>                                            <td><%= item.title %></td>                                            <td>15</td>                                            <td><%= item.sort %></td>                                            <input type="hidden" name="id" value="<%= item._id %>">                                            <td>                                                <a href="/admin/category/edit?id=<%= item._id %>" class="btn btn-default" title="编辑"><span class="fa fa-edit"></span> 编辑</a>                                                <a href="/admin/category/edit?id=<%= item._id %>" class="btn btn-default" title="删除" onclick="return confirm(‘是否删除?‘);"><span class="fa fa-trash-o"></span> 删除</a>                                            </td>                                        </tr>                                        <% }); %>                                    </tbody></table>                                </div><!-- /.box-body -->                                <div class="box-footer clearfix">                                    <ul class="pagination pagination-bg no-margin pull-right">                                        <li><a href="#">«</a></li>                                        <li><a href="#">1</a></li>                                        <li><a href="#">2</a></li>                                        <li><a href="#">3</a></li>                                        <li><a href="#">»</a></li>                                    </ul>                                </div>                            </div>                        </div>                    </div>                </section><!-- /.content -->                <% include footer.html %>

 

 

显示效果如下所示:

 

技术分享

夺命雷公狗---node.js---21之项目的构建在node+express+mongo的博客项目6之数据的遍历