nodejs 使用mongoose实现crud

index.html

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>学生列表</title>

    <!-- Bootstrap core CSS -->
    <!-- <link href="/node_modules/bootstrap/dist/css/bootstrap.css" rel="stylesheet"> -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="/public/css/main.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li>
          </ul>
          <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search...">
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li>
            <li><a href="#">Reports</a></li>
            <li><a href="#">Analytics</a></li>
            <li><a href="#">Export</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item</a></li>
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
            <li><a href="">More navigation</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Dashboard</h1>

          <div class="row placeholders">
          {{ each fruits }}
            <div class="col-xs-6 col-sm-3 placeholder">
              <img src="https://img-blog.csdnimg.cn/2022010708315841436.gif" width="200" height="200" class="img-responsive" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">{{ $value }}</span>
            </div>
           {{/each}}
          </div>

          <h2 class="sub-header">Section title</h2>
          <a href="/students/new" class="btn btn-success">添加学生</a>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>#</th>
                  <th>姓名</th>
                  <th>性别</th>
                  <th>年龄</th>
                  <th>爱好</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                {{ each students }}
                <tr>
                  <td>{{ $index + 1 }}</td>
                  <td>{{ $value.name }}</td>
                  <td>{{ $value.gender }}</td>
                  <td>{{ $value.age }}</td>
                  <td>{{ $value.hobbies }}</td>
                  <td>
                    <a href="/students/edit?_id={{ $value.id }}" class="btn btn-success">编辑</a>
                    <a href="/students/delete?_id={{ $value.id }}" class="btn btn-danger">删除</a>
                  </td>
                </tr>
                {{ /each }}
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

 new.html

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>添加学生</title>

    <!-- Bootstrap core CSS -->
    <!-- <link href="/node_modules/bootstrap/dist/css/bootstrap.css" rel="stylesheet"> -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="/public/css/main.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li>
          </ul>
          <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search...">
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li>
            <li><a href="#">Reports</a></li>
            <li><a href="#">Analytics</a></li>
            <li><a href="#">Export</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item</a></li>
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
            <li><a href="">More navigation</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h2 class="sub-header">添加学生</h2>
          <form action="/students/new" method="post">
            <div class="form-group">
              <label for="name">姓名</label>
              <input type="text" class="form-control" id="name" placeholder="姓名" name="name">
            </div>
            <div class="form-group">
              <label >性别</label>
              <div>
                <label class="radio-inline">
                <input type="radio" name="gender" id="gender" value="0"> 男
                </label>
                <label class="radio-inline">
                <input type="radio" name="gender" id="gender" value="1"> 女
                </label>
              </div>
            </div>
            <div class="form-group">
              <label for="age">年龄</label>
              <input type="number" class="form-control" id="age" placeholder="年龄" name="age">
            </div>
            <div class="form-group">
              <label for="hobbies">爱好</label>
              <input type="text" class="form-control" id="hobbies" placeholder="爱好" name="hobbies">
            </div>
          
            <button type="submit" class="btn btn-default">提交</button>
          </form>
        </div>
      </div>
    </div>
  </body>
</html>

 

 edit.html

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>添加学生</title>

    <!-- Bootstrap core CSS -->
    <!-- <link href="/node_modules/bootstrap/dist/css/bootstrap.css" rel="stylesheet"> -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="/public/css/main.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li>
          </ul>
          <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search...">
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li>
            <li><a href="#">Reports</a></li>
            <li><a href="#">Analytics</a></li>
            <li><a href="#">Export</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item</a></li>
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
            <li><a href="">More navigation</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h2 class="sub-header">编辑学生</h2>
          <form action="/students/edit" method="post">
            <div class="form-group">
              <label for="name">姓名</label>
              <input type="text" class="form-control" id="name" placeholder="姓名" name="name" value="{{ student.name }}">
              <!-- 隐藏元素用来存放不希望被用户看见,但必须得提交的内容 -->
              <input type="hidden" name="id" value="{{ student.id }}">
            </div>
            <div class="form-group">
              <label >性别</label>
              <div>
                <label class="radio-inline">
                <input type="radio" name="gender" id="gender" value="0"> 男
                </label>
                <label class="radio-inline">
                <input type="radio" name="gender" id="gender" value="1"> 女
                </label>
              </div>
            </div>
            <div class="form-group">
              <label for="age">年龄</label>
              <input type="number" class="form-control" id="age" placeholder="年龄" name="age" value="{{ student.age }}">
            </div>
            <div class="form-group">
              <label for="hobbies">爱好</label>
              <input type="text" class="form-control" id="hobbies" placeholder="爱好" name="hobbies" value="{{ student.hobbies }}">
            </div>
          
            <button type="submit" class="btn btn-default">提交</button>
          </form>
        </div>
      </div>
    </div>
  </body>
</html>

 student.js

var mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/itcast')

var Schema = mongoose.Schema

var studentSchema = new Schema({
	name: {
		type: String,
		required: true
	},
	gender: {
		type: String,
		enum: [0,1],
		default: 0
	},
	age: {
		type: Number
	},
	hobbies: {
		type: String
	}
})
//直接导出模型构造函数
module.exports = mongoose.model('Student', studentSchema)

 

router.js

// 功能:根据请求方式和请求路径设置具体的处理函数
//目的是增强代码的可维护性
var fs = require('fs')
//1、专门用来包装路由的
var express = require('express')
//2、创建一个路由
var router = express.Router()
//3、把router挂载到router路由器中

var Student = require('./student.js')
router.get('/', function (req, res)  {
	Student.find(function (err, data) {
		if (err) {
			//设置状态码为500,并告诉客户端服务错误
			return res.status(500).send('Server err.')
		}
		res.render('index.html', {
		fruits: [
			'苹果',
			'橘子',
			'梨'
		],
		students: data
		})
	})
})

router.get('/students/new', function (req,res) {
	res.render('new.html')
})
//添加学生
router.post('/students/new', function (req,res) {
	new Student(req.body).save(function (err) {
		if (err) {
			return res.status(500).send('Server err!')
		}
		res.redirect('/')
	})
})
//编辑学生
router.get('/students/edit', function (req,res) {
	Student.findById(req.query._id, function (err, stu) {
		if (err) {
			return res.status(500).send('Server err!')
		}
		res.render('edit.html', {
			student: stu
		})
	})
})
//编辑学生详细操作
router.post('/students/edit', function (req, res) {
	//post里面提交的id是name里面的id,没有_
	//而get方式提交的都是从url地址里面来的,要有下划线
	Student.findByIdAndUpdate(req.body.id, req.body, function (err) {
		if (err) {
			return res.status(500).send('Server err!')
		}
		res.redirect('/')
	})
})
//删除学生
router.get('/students/delete', function (req, res) {
	Student.remove({
		'_id': req.query._id
	}, function (err) {
		if (err) {
			return res.status(500).send('Server err!')
		}
		res.redirect('/')
	})
})

module.exports = router

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值