前端学习(1346):用户的增删改查操作3增加
生活随笔
收集整理的這篇文章主要介紹了
前端学习(1346):用户的增删改查操作3增加
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//創(chuàng)建http連接
const http = require('http');
//創(chuàng)建服務(wù)器
const app = http.createServer();
//第三方模塊導(dǎo)入
const mongoose = require('mongoose');
//獲取連接
const url = require('url');
//
const querystring = require('querystring');
//數(shù)據(jù)庫連接地址
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true }).then(() => console.log('數(shù)據(jù)庫連接成功')).catch(() => console.log('數(shù)據(jù)庫連接失敗'));
//創(chuàng)建用戶集合
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]
});
//創(chuàng)建集合
const User = mongoose.model('User', userSchema);
//添加屬性事件
app.on('request', async(req, res) => {//請(qǐng)求方式const method = req.method;//請(qǐng)求地址const { pathname } = url.parse(req.url);if (method == 'GET') {//呈現(xiàn)用戶列表頁面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>`;//循環(huán)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="請(qǐng)輸入用戶名"></div><div class="form-group"><label>密碼</label><input name="password" type="password" class="form-control" placeholder="請(qǐng)輸入密碼"></div><div class="form-group"><label>年齡</label><input name="age" type="text" class="form-control" placeholder="請(qǐng)輸入年齡"></div><div class="form-group"><label>郵箱</label><input name="email" type="text" class="form-control" placeholder="請(qǐng)輸入年齡"></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);
運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的前端学习(1346):用户的增删改查操作3增加的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hb100 微波雷达arduino_HB
- 下一篇: 使用 Google Analytics