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發前端看一下先就這樣吧,
一鍵三連一鍵三連
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/263837.html
標籤:其他
上一篇:8--請求轉發和重定向
