const mongoose = require('mongoose')
mongoose
.connect('mongodb://localhost/demo', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('数据库连接成功'))
.catch(() => console.log('数据库连接失败'))
const mongoose = require('mongoose')
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)
module.exports = User;
const http = require('http')
const querystring = require('querystring')
require('./model/index.js')
const User = require('./model/user')
const url = require('url')
const app = http.createServer()
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()
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 hobbies = ['eating', 'cooking', 'singing', 'dancing', 'writing', 'running']
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>
`;
hobbies.forEach(item => {
add += `
<label class="checkbox-inline">
<input type="checkbox" value="${item}" name="hobbies"> ${item}
</label>
`
})
add += `
</div>
</div>
<button type="submit" class="btn btn-primary">添加用户</button>
</form>
</div>
</body>
</html>
`
res.end(add)
} else if (pathname == '/modify') {
let hobbies = ['eating', 'cooking', 'singing', 'dancing', 'writing', 'running']
let user = await User.findOne({
_id: query.id
})
console.log(user)
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="请填写用户名">
</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" name="hobbies" checked> ${item}
</label>
`
} else {
modify += `
<label class="checkbox-inline">
<input type="checkbox" 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 = ''
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()
})
}
if (pathname == '/modify') {
let formData = ''
req.on('data', (param) => {
formData += param
})
req.on('end', async () => {
let user = querystring.parse(formData)
await User.updateOne({
_id: query.id
}, user)
res.writeHead(301, {
Location: '/list',
})
res.end()
})
}
}
})
app.listen(3000)