\
service.js
//引入http模块
const http = require('http');
//创建网站服务器
const app = http.createServer();
//
require('./connect.js')
const Student = require('./user.js')
app.on('request', (req, res) => {res.end('ok');
});
app.listen(3000);
console.log('服务器启动成功');
user.js
const mongoose = require('mongoose');const studentsSchema = new mongoose.Schema({name: {type: String,required: true,minlength: 2,maxlength: 10},age: {type: Number,min: 10,max: 25},sex: {type: String},email: String,hobbies: [String],collage: String,enterDate: {type: Date,default: Date.now}});
const Student = mongoose.model('Student', studentsSchema);module.exports = Student;
connect.js
//链接数据库
const mongoose = require('mongoose');
//链接数据库
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true }).then(() => console.log('数据库安装成功')).catch(() => console.log('数据库链接失败'))