mongodb用户信息管理案例
一,搭建网站服务器,实现客户端和服务器端的通信
// 创建服务器
const http=require('http')
const app=http.createServer()
//为服务器对象添加请求事件,如果客户端有请求就触发
app.on('request',(req,res)=>{res.end("ok")})
// 监听端口
app.listen(3000)
console.log("服务器已启用")
二,完成到一个文件夹下:
// 创建服务器
const http=require('http')
const app=http.createServer()
const url= require('url')
const querystring=require('querystring')
//引入mongoose
const mongoose=require('mongoose')
//数据库连接 27017是数据库的默认端口
mongoose.connect('mongodb://localhost/playground',{useNewUrlParser: true,useUnifiedTopology: true }).then(()=>console.log('数据库连接成功')).catch(err=>console.log(err,'数据库连接失败'))
//创建用户集合的规则
const userSchema=new mongoose.Schema({name:{type:String,require:true,minlength:2,maxlength:20,},age:{type:Number,min:18,max:80,},password:String,email:String,hobbies:[String] //是一个数组,数组中的元素是string
})
//创建集合,返回集合构造函数
const User=mongoose.model('User',userSchema)//为服务器对象添加请求事件,如果客户端有请求就触发
app.on('request',async (req,res)=>{//请求方式const method= req.method.toLowerCase()let{query,pathname}=url.parse(req.url,true)if (method=='get'){//查询用户信息let users= await User.find()//html字符串if (pathname=='/list'){let list=`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>用户列表</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"></head><body><div class="container"><h6><a href="/add" class="btn btn-primary">添加用户</a></h6><table class="table table-striped table-bordered"><tr><td>用户名</td><td>年龄</td><td>爱好</td><td>邮箱</td><td>操作</td></tr>`;users.forEach(item=>{list += `<tr><td>${item.name}</td><td>${item.age}</td><td>`;item.hobbies.forEach(item =>{list+=`<span>${item}</span>`});list +=`</td><td>${item.email}</td><td><a href="/remove?id=${item._id}" class="btn btn-danger btn-xs">删除</a><a href="/modify?id=${item._id}" class="btn btn-success btn-xs">修改</a></td></tr>`});list +=`</table></div></body></html>`;res.end(list)}else if(pathname=='/add'){let add=`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>用户列表</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"></head><body><div class="container"><h3>添加用户</h3><form method="post" action="/add"><div class="form-group"><label>用户名</label><input name="name" type="text" class="form-control" placeholder="请填写用户名"></div><div class="form-group"><label>密码</label><input name="password" type="password" class="form-control" placeholder="请输入密码"></div><div class="form-group"><label>年龄</label><input name="age" type="text" class="form-control" placeholder="请填写年龄"></div><div class="form-group"><label>邮箱</label><input name="email" type="email" class="form-control" placeholder="请填写邮箱"></div><div class="form-group"><label>请选择爱好</label><div><label class="checkbox-inline"><input type="checkbox" value="足球" name="hobbies"> 足球</label> <label class="checkbox-inline"> <input type="checkbox" value="篮球" name="hobbies"> 篮球</label> <label class="checkbox-inline"> <input type="checkbox" value="橄榄球" name="hobbies"> 橄榄球</label> <label class="checkbox-inline"> <input type="checkbox" value="敲代码" name="hobbies"> 敲代码</label> <label class="checkbox-inline"> <input type="checkbox" value="抽烟" name="hobbies"> 抽烟</label> <label class="checkbox-inline"> <input type="checkbox" value="喝酒" name="hobbies"> 喝酒</label> <label class="checkbox-inline"> <input type="checkbox" value="烫头" name="hobbies"> 烫头</label></div></div><button type="submit" class="btn btn-primary">添加用户</button></form></div></body></html>`res.end(add)}else if(pathname=='/modify'){let user=await User.findOne({_id:query.id})let hobbies=['足球','篮球','橄榄球','敲代码','抽烟','喝酒','烫头']let modify=`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>用户列表</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"></head><body><div class="container"><h3>修改用户</h3><form method="post" action="/modify?id=${user.id}"><div class="form-group"><label>用户名</label><input value=${user.name} name="name" type="text" class="form-control" placeholder="222"></div><div class="form-group"><label>密码</label><input value=${user.password} name="password" type="password" class="form-control" placeholder="请输入密码"></div><div class="form-group"><label>年龄</label><input value=${user.age} name="age" type="text" class="form-control" placeholder="请填写邮箱"></div><div class="form-group"><label>邮箱</label><input value=${user.email} name="email" type="email" class="form-control" placeholder="请填写邮箱"></div><div class="form-group"><label>请选择爱好</label><div>`hobbies.forEach(item =>{//判断当前循环项在不在用户的爱好数组里面let isHobby=user.hobbies.includes(item)if(isHobby){modify+=`<label class="checkbox-inline"><input type="checkbox" value=${item} name="hobbies" checked> ${item}`}else{modify+=`<label class="checkbox-inline"><input type="checkbox" value=${item} name="hobbies"> ${item}</label>`}})modify +=`</div></div><button type="submit" class="btn btn-primary">修改用户</button></form></div></body></html>`res.end(modify)}else if(pathname=='/remove'){await User.findOneAndDelete({_id:query.id})res.writeHead(301,{Location:'/list'})res.end()}}else if(method=='post'){//用户添加功能if (pathname=='/add'){//接收用户提交的信息let formData=''//接收post参数req.on('data',param=>{formData +=param})//post参数接收完毕req.on('end',async()=>{let user=querystring.parse(formData)//将用户提交的信息添加到数据库await User.create(user)//重定向到列表页,301代表重定向res.writeHead(301,{Location:'/list'})res.end()})}else if(pathname =='/modify'){//接收用户提交的信息let formData=''//接收post参数req.on('data',param=>{formData +=param})//post参数接收完毕req.on('end',async()=>{let user=querystring.parse(formData)//将用户提交的信息添加到数据库await User.updateOne({_id: query.id},user)//重定向到列表页,301代表重定向res.writeHead(301,{Location:'/list'})res.end()})}}})
// 监听端口
app.listen(3000)
console.log("服务器已启用")
三,这样都写在一个文件下,违背了我们模块化开发的理念,要进行模块化开发:
就比如说,数据库的连接可以放到index.js里面,用户集合的创建也可以单独放一个。
通常做项目时,在根目录下会有一个 叫做model的文件夹,我们会放置和数据库相关的一些操作。
于是app.js:
// 创建服务器
const http=require('http')
const app=http.createServer()
const url= require('url')
const querystring=require('querystring')require('./model/index.js')
const User=require('./model/user.js')//为服务器对象添加请求事件,如果客户端有请求就触发
app.on('request',async (req,res)=>{//请求方式const method= req.method.toLowerCase()let{query,pathname}=url.parse(req.url,true)if (method=='get'){//查询用户信息let users= await User.find()//html字符串if (pathname=='/list'){let list=`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>用户列表</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"></head><body><div class="container"><h6><a href="/add" class="btn btn-primary">添加用户</a></h6><table class="table table-striped table-bordered"><tr><td>用户名</td><td>年龄</td><td>爱好</td><td>邮箱</td><td>操作</td></tr>`;users.forEach(item=>{list += `<tr><td>${item.name}</td><td>${item.age}</td><td>`;item.hobbies.forEach(item =>{list+=`<span>${item}</span>`});list +=`</td><td>${item.email}</td><td><a href="/remove?id=${item._id}" class="btn btn-danger btn-xs">删除</a><a href="/modify?id=${item._id}" class="btn btn-success btn-xs">修改</a></td></tr>`});list +=`</table></div></body></html>`;res.end(list)}else if(pathname=='/add'){let add=`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>用户列表</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"></head><body><div class="container"><h3>添加用户</h3><form method="post" action="/add"><div class="form-group"><label>用户名</label><input name="name" type="text" class="form-control" placeholder="请填写用户名"></div><div class="form-group"><label>密码</label><input name="password" type="password" class="form-control" placeholder="请输入密码"></div><div class="form-group"><label>年龄</label><input name="age" type="text" class="form-control" placeholder="请填写年龄"></div><div class="form-group"><label>邮箱</label><input name="email" type="email" class="form-control" placeholder="请填写邮箱"></div><div class="form-group"><label>请选择爱好</label><div><label class="checkbox-inline"><input type="checkbox" value="足球" name="hobbies"> 足球</label> <label class="checkbox-inline"> <input type="checkbox" value="篮球" name="hobbies"> 篮球</label> <label class="checkbox-inline"> <input type="checkbox" value="橄榄球" name="hobbies"> 橄榄球</label> <label class="checkbox-inline"> <input type="checkbox" value="敲代码" name="hobbies"> 敲代码</label> <label class="checkbox-inline"> <input type="checkbox" value="抽烟" name="hobbies"> 抽烟</label> <label class="checkbox-inline"> <input type="checkbox" value="喝酒" name="hobbies"> 喝酒</label> <label class="checkbox-inline"> <input type="checkbox" value="烫头" name="hobbies"> 烫头</label></div></div><button type="submit" class="btn btn-primary">添加用户</button></form></div></body></html>`res.end(add)}else if(pathname=='/modify'){let user=await User.findOne({_id:query.id})let hobbies=['足球','篮球','橄榄球','敲代码','抽烟','喝酒','烫头']let modify=`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>用户列表</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"></head><body><div class="container"><h3>修改用户</h3><form method="post" action="/modify?id=${user.id}"><div class="form-group"><label>用户名</label><input value=${user.name} name="name" type="text" class="form-control" placeholder="222"></div><div class="form-group"><label>密码</label><input value=${user.password} name="password" type="password" class="form-control" placeholder="请输入密码"></div><div class="form-group"><label>年龄</label><input value=${user.age} name="age" type="text" class="form-control" placeholder="请填写邮箱"></div><div class="form-group"><label>邮箱</label><input value=${user.email} name="email" type="email" class="form-control" placeholder="请填写邮箱"></div><div class="form-group"><label>请选择爱好</label><div>`hobbies.forEach(item =>{//判断当前循环项在不在用户的爱好数组里面let isHobby=user.hobbies.includes(item)if(isHobby){modify+=`<label class="checkbox-inline"><input type="checkbox" value=${item} name="hobbies" checked> ${item}`}else{modify+=`<label class="checkbox-inline"><input type="checkbox" value=${item} name="hobbies"> ${item}</label>`}})modify +=`</div></div><button type="submit" class="btn btn-primary">修改用户</button></form></div></body></html>`res.end(modify)}else if(pathname=='/remove'){await User.findOneAndDelete({_id:query.id})res.writeHead(301,{Location:'/list'})res.end()}}else if(method=='post'){//用户添加功能if (pathname=='/add'){//接收用户提交的信息let formData=''//接收post参数req.on('data',param=>{formData +=param})//post参数接收完毕req.on('end',async()=>{let user=querystring.parse(formData)//将用户提交的信息添加到数据库await User.create(user)//重定向到列表页,301代表重定向res.writeHead(301,{Location:'/list'})res.end()})}else if(pathname =='/modify'){//接收用户提交的信息let formData=''//接收post参数req.on('data',param=>{formData +=param})//post参数接收完毕req.on('end',async()=>{let user=querystring.parse(formData)//将用户提交的信息添加到数据库await User.updateOne({_id: query.id},user)//重定向到列表页,301代表重定向res.writeHead(301,{Location:'/list'})res.end()})}}})
// 监听端口
app.listen(3000)
console.log("服务器已启用")
然后index.js:
//引入mongoose
const mongoose=require('mongoose')
//数据库连接 27017是数据库的默认端口
mongoose.connect('mongodb://localhost/playground',{useNewUrlParser: true,useUnifiedTopology: true }).then(()=>console.log('数据库连接成功')).catch(err=>console.log(err,'数据库连接失败'))
最后user.js:
//引入mongoose
const mongoose=require('mongoose')
//创建用户集合的规则
const userSchema=new mongoose.Schema({name:{type:String,require:true,minlength:2,maxlength:20,},age:{type:Number,min:18,max:80,},password:String,email:String,hobbies:[String] //是一个数组,数组中的元素是string
})
//创建集合,返回集合构造函数
const User=mongoose.model('User',userSchema)
//开放user
module.exports=User;
实现的效果:
mongodb用户信息管理案例相关推荐
- 小编程(三):用户登录注册界面开发及用户信息管理案例代码详解
用户登录注册界面开发及用户信息管理案例详解 刚开始接触Android编程,这算是我写的第一个简单工程,主要功能有:用户登录.注册.注销.修改密码.记住密码共5个基本操作,其内容涉及到以下几点: 1:B ...
- MongoDB用户授权和管理
2019独角兽企业重金招聘Python工程师标准>>> MongoDB用户授权和管理 2017年02月15日 15:40:04 奋斗吧_攻城狮 阅读数:6974 标签: mongod ...
- mongodb用户管理简单记录
Mongodb用户分为三种 1.全局用户 2.数据库对应用户 3.只读用户 查看所有的数据库 > show dbs admin 0.078GB book_blog 0.078GB local 0 ...
- ucla ai_UCLA的可持续性:用户体验案例研究
ucla ai Role: UX Researcher / UX Designer / Critical-thinker 角色: UX研究人员/ UX设计人员/批判性思维者 Scope: 4 week ...
- 用户体验改善案例_用户体验案例研究:建立更好的体验(重新设计“和平航空”网站)...
用户体验改善案例 by Peace Ojemeh (Perrie) 由Peace Ojemeh(Perrie) 用户体验案例研究:建立更好的体验(重新设计"和平航空"网站) (A ...
- html写学生信息管理,vue实现简单学生信息管理案例
学生信息管理 #app{ margin: 10px; } 学号: 姓名: 搜索姓名关键字: 学号姓名添加时间操作 {{item.stuNo}}{{item.name}}{{item.cTime | d ...
- go mongodb排序查询_Kotlin与MongoDB整合CURD案例详解
1.mongodb的低版本bson无法转换类型 比如MongoDB数据库表的字段类型为Decimal,实体类用String去定义就会报如下错误 No converter found capablof ...
- web端业务数据管理平台+Axure运营数据管理平台+月度数据统计分析+年度排行榜数据统计页面分析+运营大数据统计管理后台+用户信息管理+Axure通用web端高保真交互业务数据管理平台
作品介绍:原型内容包含:web端业务数据管理平台+Axure运营数据管理平台+月度数据统计分析+年度排行榜数据统计页面分析+运营大数据统计管理后台+用户信息管理+Axure通用web端高保真交互业务数 ...
- 移动端业务数据管理平台+健康管理平台+banner管理+图标管理+订单管理+门店内容管理+用户信息管理+版本更新管理Axure通用web端高保真交互app业务数据管理平台
作品介绍:移动端业务数据管理平台+健康管理平台+banner管理+图标管理+订单管理+闪屏信息管理+门店内容管理+用户信息管理+版本更新管理Axure通用web端高保真交互app业务数据管理平台 ap ...
- 【探花交友】保存用户信息、上传用户头像、用户信息管理
文章目录 1.3.保存用户信息 1.4.上传用户头像 2.用户信息管理 2.1.查询用户资料 2.2.更新用户资料 1.3.保存用户信息 1.3.1.接口文档 YAPI接口地址:http://192. ...
最新文章
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
- 模块就是一个普通的python程序文件_Python-模块和包
- 【机器学习基础】数学推导+纯Python实现机器学习算法18:奇异值分解SVD
- Java工具实现无水印批量下载
- 线性表中顺序表基本运算的实现---数据结构(C)
- I/O读写的另一种方式-NIO
- 分享一个NI软件卸载工具
- Junos 操作系统
- 阿里 P7 到底该具备什么样的能力?
- 数据仓库指北(文末附PDF下载)
- SysML-Sec A model Driven Approach for Designing Safe and Secure Systems
- android 滑动取值_Android中滑屏实现
- Linux力挺微博世
- 智慧校园:校务助手微信小程序端源码
- 激励的最佳状态,是让员工在最需要激励的时候,获得最大的心理满足
- 基于CIM的馈线建模和应用(论文学习)
- 干货分享:Windows资源管理器无限重启?解决方法竟然是…
- Log4j2的MDC详解
- WinXP原版与VOL版的区别
- Python: 既约分数