const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test01', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
- 创建集合规则并利用该规则创建学生集合(…/model/user):
const mongoose = require('mongoose');
const stuSchema = new mongoose.Schema({
name: {
type: String,
required: [true, '请输入姓名'],
minlength: 2,
maxlength: 10
},
age: {
type: Number,
min: 18,
max: 60
},
sex: String,
email: String,
hobbies: [String],
collage: String,
enDate: {
type: Date,
default: Date.now
}
});
const Student = mongoose.model('student', stuSchema);
module.exports = Student;
const getRouter = require('router');
const Student = require('../model/user');
const query = require('querystring');
const template = require('art-template');
const router = getRouter();
router.get('/add', (req, res) => {
let html = template('add.art', {});
res.end(html);
});
router.get('/list', async(req, res) => {
let students = await Student.find();
let html = template('list.art', {
students: students
});
res.end(html);
});
router.post('/add', (req, res) => {
let formData = '';
req.on('data', param => {
formData += param;
});
req.on('end', async() => {
await Student.create(query.parse(formData));
res.writeHead(301, {
'Location': '/list'
});
res.end();
})
});
module.exports = router;
const http = require('http');
require('./model/connect');
const template = require('art-template');
const path = require('path');
const serveStatic = require('serve-static');
const dateformat = require('dateformat');
const router = require('./route');
const app = http.createServer();
template.defaults.root = path.join(__dirname, 'view');
template.defaults.imports.dateformat = dateformat;
const serve = serveStatic(path.join(__dirname, 'public'));
app.on('request', (req, res) => {
router(req, res, () => {});
serve(req, res, () => {});
});
app.listen(80);
console.log('服务器启动成功');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生档案</title>
<link rel="stylesheet" href="./add.css">
</head>
<body>
<div class="box">
<div class="title">
<h2>学生档案</h2>
</div>
<form action="/add" method="post">
<ul>
<li>
<label for="name">姓名:</label>
<input type="text" class="inp" name="name" placeholder="请输入姓名" />
</li>
<li>
<label for="age">年龄:</label>
<input type="number" class="inp" name="age" placeholder="请输入年龄" />
</li>
<li class="sex">
<label for="sex">性别:</label>
<input type="radio" name="sex" value='0' /> 男
<input type="radio" name="sex" value='1'/> 女
</li>
<li>
<label for="email">邮箱地址:</label>
<input type="email" class="inp" name="email" placeholder="请输入邮箱地址" />
</li>
<li class="hob">
<label for="hobbies">爱好:
<input type="checkbox" name="hobbies" value="敲代码"/> 敲代码
<input type="checkbox" name="hobbies" value="打篮球"/> 打篮球
<input type="checkbox" name="hobbies" value="睡觉"/> 睡觉
</label>
</li>
<li class="coll">
<label for="collage">所属学院:
<select name="collage" class="inp">
<option value="前端与移动开发">前端与移动开发</option>
<option value="PHP">PHP</option>
<option value="JAVA">JAVA</option>
<option value="Android">Android</option>
<option value="IOS">IOS</option>
<option value="UI设计">UI设计</option>
<option value="C++">C++</option>
</select>
</label>
</li>
<li>
<label for="enDate">入学日期:</label>
<input type="date" class="inp" name="enDate" />
</li>
<li class="sub">
<label class="btn">
<input type="submit" value="提交" class="subBtn"></input>
</label>
</li>
</ul>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学员信息</title>
<link rel="stylesheet" href="./list.css">
</head>
<body>
<table>
<caption>学员信息</caption>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>邮箱地址</th>
<th>爱好</th>
<th>所属学院</th>
<th>入学时间</th>
</tr>
{{each students}}
<tr>
<td>{{$value.name}}</td>
<td>{{$value.age}}</td>
<td>{{$value.sex == '0' ? '男' : '女'}}</td>
<td>{{$value.email}}</td>
<td>
{{each $value.hobbies}}
<span>{{$value}}</span>
{{/each}}
</td>
<td>{{$value.collage}}</td>
<td>{{dateformat($value.enDate, 'yyyy-mm-dd')}}</td>
</tr>
{{/each}}
</table>
</body>
</html>