1.下载mongoose模块
2.在项目中创建数据库操作模块:sql(文件夹)/db.js(连接数据库) + pro.js(创建集合) + sql.js(数据库增删改查的封装)
3.创建集合之前,先设计集合结构(pro.js)
4.将集合模块暴露,准备实现数据的操作
5.在数据库操作的pro.js 引入数据库的连接模块 ,创建集合,集合中写入设计的模板,暴露模块
6.数据在路由文件pro.js中插入,所以刚刚数据库操作模块都要引入,我做的是商品插入,所以每一个要有唯一的id与之对应,我这里用了下载了node-uuid工具,使用uuid.v1()生成唯一的id,后面操作商品用的到,在这个商品信息的路由中, 每次请求时先获取所有数据,然后再讲数据传给页面模板渲染
7.数据先是用form表单发送给后端,后端post接收后,经过数据类型处理存入数据库,存完之后,第一篇说过,新数据插入了,要路由重定向等于刷新了页面,把新的数据显示出来。
8.数据自己一条条插效率太低了,这里用了别人封装好的一个工具,可以插入execl的表格,需要简单的一些处理,下载工具node-xlsx ,下载完之后在pro路由中建立子路由,pro的页面模板创建a标签,a标签跳转的地址就是子路由的地址,路由pro设置子路由,进行数据处理,需要用到xlsx.parse()解析数据:
xlsx.parse(__dirname + “/…/excel/pro.xlsx”)
9.dirname 是当前文件的绝对路径后面进行操作找到表格,表格数据还是需要解析的,具体要根据这个数据的格式。可以用res的send发过去看一下再决定操作,拿一个空数组,for循环遍历刚刚解析的数据利用数组的方法添加进去,然后添加到已经创建集合且暴露的pro.js中,添加完记得路由的重定向,在页面模板记得解析这些数据,下面放相关代码
db.js
const mongoose = require("mongoose");
mongoose.connect('mongodb://127.0.0.1:27017/wys',{
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on("connected",()=>{
console.log("连接成功")
})
mongoose.connection.on("disconnected",()=>{
console.log("连接断开")
})
mongoose.connection.on("error",()=>{
console.log("连接错误")
})
module.exports = mongoose;
pro.js
const mongoose = require("./db");
// 创建集合对象
const proSchema = new mongoose.Schema({
proId:{type:String},
brand:{type:String},
logo:{type:String},
kind:{type:String},
img:{type:String},
wname:{type:String},
price:{type:Number},
sell:{type:Number},
rest:{type:Number},
discount:{type:Number},
point:{type:Number}
})
module.exports = mongoose.model("pros", proSchema);
pro.ejs
<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>AdminLTE 2 | Starter</title>
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="/stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="/stylesheets/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="/stylesheets/skins/skin-blue.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="/stylesheets/ionicons.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="/stylesheets/AdminLTE.min.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<!--
BODY TAG OPTIONS:
=================
Apply one or more of the following classes to get the
desired effect
|---------------------------------------------------------|
| SKINS | skin-blue |
| | skin-black |
| | skin-purple |
| | skin-yellow |
| | skin-red |
| | skin-green |
|---------------------------------------------------------|
|LAYOUT OPTIONS | fixed |
| | layout-boxed |
| | layout-top-nav |
| | sidebar-collapse |
| | sidebar-mini |
|---------------------------------------------------------|
-->
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<!-- Main Header -->
<%- include("./header.ejs") %>
<!-- Left side column. contains the logo and sidebar -->
<%- include("./aside.ejs") %>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>商品信息管理</h1>
</section>
<!-- Main content -->
<section class="content container-fluid">
<div class="box">
<div class="box-header">
<h3 class="box-title">商品信息展示区域</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<div id="example1_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
<div class="row">
<div class="col-sm-4">
<div class="dataTables_length" id="example1_length">
<label>Show <select name="example1_length"
aria-controls="example1" class="form-control input-sm">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select> entries</label>
</div>
</div>
<div class="col-sm-4">
<div id="example1_filter" class="dataTables_filter"><label>Search:<input type="search"
class="form-control input-sm" placeholder="" aria-controls="example1"></label></div>
</div>
<div class="col-sm-4">
<a href="/pro/add" class="btn btn-info" style="float:right">添加信息</a>
<a href="/pro/upload" class="btn btn-info" style="float:right">导入数据</a>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table id="example1" class="table table-bordered table-striped dataTable" role="grid"
aria-describedby="example1_info">
<thead>
<tr role="row">
<th width=50>序号</th>
<th>品牌</th>
<th>LOGO</th>
<th>分类</th>
<th>图片</th>
<th width=200>名称</th>
<th>价格</th>
<th>销量</th>
<th>库存</th>
<th>折扣</th>
<th>评分</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<% for(var i=0;i<data.length;i++){ %>
<tr>
<td><%= i+1 %></td>
<td><%= data[i].brand %></td>
<td><img width=100 src="<%= data[i].logo %>" alt=""></td>
<td><%= data[i].kind %></td>
<td><img width=100 src="<%= data[i].img %>" alt=""></td>
<td><%= data[i].wname %></td>
<td><%= data[i].price %></td>
<td><%= data[i].sell %></td>
<td><%= data[i].rest %></td>
<td><%= data[i].discount %></td>
<td><%= data[i].point %></td>
<td>
<button>删</button>
<button>改</button>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<div class="dataTables_info" id="example1_info" role="status" aria-live="polite">Showing 1 to 10 of 57
entries</div>
</div>
<div class="col-sm-7">
<div class="dataTables_paginate paging_simple_numbers" id="example1_paginate">
<ul class="pagination">
<li class="paginate_button previous disabled" id="example1_previous"><a href="#"
aria-controls="example1" data-dt-idx="0" tabindex="0">Previous</a></li>
<li class="paginate_button active"><a href="#" aria-controls="example1" data-dt-idx="1"
tabindex="0">1</a></li>
<li class="paginate_button next" id="example1_next"><a href="#" aria-controls="example1"
data-dt-idx="7" tabindex="0">Next</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!-- /.box-body -->
</div>
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<!-- Main Footer -->
<%- include("./footer.ejs") %>
</div>
<!-- ./wrapper -->
<!-- jQuery 3 -->
<script src="/javascripts/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="/javascripts/bootstrap.min.js"></script>
<!-- AdminLTE App -->
<script src="/javascripts/adminlte.min.js"></script>
</body>
</html>
sql.js前一篇有我就不放了(调了一下午的bug就是没想过自己封装出了问题,漏了个参数,唉)
解析数据,要根据样式操作哦用send发前端看一下先就这样吧。
一键三连一键三连