//创建http连接
const http = require('http');
//创建服务器
const app = http.createServer();
//第三方模块导入
const mongoose = require('mongoose');
//获取连接
const url = require('url');
//
const querystring = require('querystring');
//数据库连接地址
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true }).then(() => console.log('数据库连接成功')).catch(() => console.log('数据库连接失败'));
//创建用户集合
const userSchema = new mongoose.Schema({name: {type: String,required: true,minlength: 2,maxlength: 20},age: {type: Number,min: 18,max: 80},password: String,email: String,hobbies: [String]
});
//创建集合
const User = mongoose.model('User', userSchema);
//添加属性事件
app.on('request', async(req, res) => {//请求方式const method = req.method;//请求地址const { pathname } = url.parse(req.url);if (method == 'GET') {//呈现用户列表页面if (pathname == '/list') {let users = await User.find();console.log(users);let list = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></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="">删除</a>|<a href="">修改</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"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></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="text" class="form-control" placeholder="请输入年龄"></div><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><button type="submit">添加用户</button></div></form></div></body></html>`res.end(add);}} else if (method == 'POST') {//接受用户提交信息if (pathname == '/add') {let formData = '';req.on('data', param => {formData += param;})req.on('end', async() => {let user = querystring.parse(formData);await User.create(user);res.writeHead(301, {Location: 'list'});res.end();})}}url.parse(req.url);/* res.end('ok'); */
})
app.listen(3000);

运行结果

前端学习(1346):用户的增删改查操作3增加相关推荐

  1. 前端学习(1350):用户的增删改查操作7增删改查

    demo25.js //创建http连接 const http = require('http'); //创建服务器 const app = http.createServer(); //第三方模块导 ...

  2. 前端学习(1349):用户的增删改查操作6删除

    //创建http连接 const http = require('http'); //创建服务器 const app = http.createServer(); //第三方模块导入 const mo ...

  3. 前端学习(1348):用户的增删改查操作5修改

    //创建http连接 const http = require('http'); //创建服务器 const app = http.createServer(); //第三方模块导入 const mo ...

  4. 前端学习(1347):用户的增删改查操作4修改

    //创建http连接 const http = require('http'); //创建服务器 const app = http.createServer(); //第三方模块导入 const mo ...

  5. 前端学习(1345):用户的增删改查操作2

    //创建http连接 const http = require('http'); //创建服务器 const app = http.createServer(); //第三方模块导入 const mo ...

  6. 前端学习(1344):用户的增删改查操作1

    const http = require('http'); const mongoose = require('mongoose');//数据库连接 mongoose.connect('mongodb ...

  7. python子进程kerberos_Python实现Kerberos用户的增删改查操作

    1.首先模拟python类似shell命令行操作的接口: python安装subprocess(本地).paramiko(SSH远程) #-*- coding: UTF-8 -*- #!/usr/bi ...

  8. 数据库表的增删改查操作

    目录 准备工作 一.增加操作 1.全列插入 2.多行插入 3.指定列插入 二.查询操作 1.全列查询和指定列查询 2.对查询的字段起别名,查询的字段为表达式 3.对于查询结果进行去重 4.对查询的结果 ...

  9. 数据库多表的增删改查操作

    数据库多表的增删改查操作: 增加操作(一对多)--- 一对多形式的表的建立: models.py: from django.db import models # Create your models ...

最新文章

  1. vim+vundle配置
  2. Ubuntu 12.04 64bit上安装Apache Traffic Server 4.1.2
  3. 计算机英语文章阅读网站,关于电脑的英语美文阅读
  4. 虫洞协议和闪电网络如何混为一谈?
  5. shiro表单认证(系统默认的form认证器)
  6. Dict Set usage
  7. rocketmq存储结构_阿里专家分享内部绝密RocketMQ核心原理与最佳实践笔记
  8. 计算机的网络默认下拉列表出不来,网页下拉菜单打不开,怎么解决?
  9. 20190313_C#反转绘制字符串
  10. 基于RabbitMQ消息队列的分布式事务解决方案 - MQ分布式消息中间件实战
  11. 拉格朗日插值多项式及其余项
  12. 最小二乘支持向量机(LSSVM)学习记录
  13. 高德地图通过经纬度定位并打上标记
  14. 单片机及开发板介绍(学习笔记)
  15. android手机连不上wifi密码,修改wifi密码后手机连不上_修改wifi密码后手机不能上网-192路由网...
  16. Fresher的完美文本
  17. C++新老多线程写法的比较【水一下】
  18. windows性能计数器
  19. 对12306车票数据的提取
  20. A. One and Two

热门文章

  1. 利用FPGA加速实现高性能计算
  2. CGContextRef CIImageRef详解
  3. lightoj 1020 (博弈水题)
  4. Android UI开发第二十九篇——Android中五种常用的menu(菜单)
  5. java中同步组件_Java并发编程(自定义同步组件)
  6. java怎么处理ajax请求,java怎么用ajax请求?jquery ajax请求后台的简单例子
  7. hashcode java_java 的Object类的hashcode()方法具体是怎么实现的?
  8. Ubuntu16.04中php如何切换版本
  9. python-上传下载文件
  10. centos配置ssh免密码登录后仍要输入密码的解决方法