//创建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, query } = url.parse(req.url, true);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="/modify?id=${item._id}">修改</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 (pathname == '/modify') {let user = await User.findOne({ _id: query.id });let hobbies = ['足球', '篮球', '游戏', '娱乐', '爬山', '划船'];let modify = `<!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 value='${user.name}' name="name" type="text" class="form-control" placeholder="请输入用户名"></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="text" class="form-control" placeholder="请输入年龄"></div><div></html>`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}</label>`} else {modify += `<label class="checkbox-inline"><input type="checkbox" value="${item}" name="hobbies">${item}</label>`}})modify += ` <button type="submit">修改用户</button></div></form></div>
</body>`res.end(modify);}} 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);
运行结果