自用_基于MongoDB的用户信息列表案例

1:创建项目(npm init -y+安装mongoose)

2:于app.js中建服务器(实现客户端与服务器端的通信)

const http = require('http');
// 创建服务器
const app = http.createServer();
// 为服务器对象添加请求事件
app.on('request', (req, res) => {
   
  res.end('ok');
});
// 监听端口
app.listen(3000);
console.log('服务器已启动,监听3000端口');

主:将用户信息和表格HTML进行拼接并将拼接结果响应回客户端

const url = require('url');
const querystring = require('querystring');
// 为服务器对象添加请求事件
app.on('request', async (req, res) => {
   
	// 请求方式
	const method = req.method;
	// 请求地址
	const {
    pathname, query } = url.parse(req.url, true);
	if (method == 'GET') {
   
		// 呈现用户列表页面
		if (pathname == '/list') {
   
		
         } else if (pathname == '/add') {
   

         } else if (pathname == '/modify') {
   

         } else if (pathname == '/remove') {
   

         }
	} else if (method == 'POST') {
   //表单提交后会发送POST请求,所以先在POST中判断请求地址
	// 用户添加功能
  		if (pathname == '/add') {
   
	    // 接收用户提交的信息
    		let formData = '';
	    // 接受post参数
    		req.on('data', param => {
   
     		 formData += param;
    })
   // post参数接收完毕
    req.on('end', async () => {
   
    // console.log(formData);
      let user = querystring.parse(formData)
      // console.log(user);
      // 将用户提交的信息添加到数据库中
      await User.create(user);
      // 301代表重定向
      // location 跳转地址
      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代表重定向
      // location 跳转地址
      res.writeHead(301, {
   
        Location: '/list'
      });
      res.end();
    })
}

		  }
     }
     res.end('ok');
});

3:于app.js中连数据库(创建用户集合,向集合中插入文档)

较好的操作是:1.将数据库连接的代码抽取到model\index.js
2.将操作用户集合的代码抽取到model\user.js

导入user.json中的数据:“D:\MongoDB\bin\mongoimport.exe” -d playground -c users --file ./user.json

model\index.js中写如下:+ 在app.js写:require(‘./model/index.js’);


const mongoose = require('mongoose');
// 数据库连接 27017是mongodb数据库的默认端口
mongoose.connect('mongodb://localhost/playground', {
    useNewUrlParser: true })
	.then(() => console.log('数据库连接成功'))
	.catch(() => console.log('数据库连接失败'));

model\user.js中写如下:+ 在app.js写const User = require(‘./model/user’);

const mongoose = require('mongoose');
	// 创建用户集合规则
const userSchema = new mongoose.Schema({
   
	name: {
   
		type: String,
		required: true,
		minlength: 1,
		maxlength: 20
		},
	age: {
   
		type: Number,
		min: 0,
		max: 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值