01 项目初始化
npm init -y
git init
02 安装项目依赖
npm i express mongoose --save
03 新建文件目录
04 生成APP服务
var express = require('express')
var router = require('./router/router.js')
var bodyParser = require('body-parser')
var app = express()
app.engine('html', require('express-art-template'))
app.use('/node_modules/',express.static('./node_modules/'))
app.use('/public/',express.static('./public/'))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(router)
app.listen(3000,() => {
console.log('Server is running...')
})
var express = require('express')
var Student = require('../mongoose')
var router = express.Router()
router.get('/students', (req, res) => {
Student.find(function (err, students) {
if (err) {
return res.status(500).send('Server error.')
}
res.render('index.html', {
fruits: [
'苹果',
'橘子',
'香蕉'
],
students: students
})
})
})
router.get('/students/new', (req, res) => {
res.render('new.html')
})
router.post('/students/new', (req, res) => {
new Student(req.body).save(function (err) {
if (err) {
return res.status(500).send('Server error.')
}
res.redirect('/students')
})
})
router.get('/students/edit', (req, res) => {
Student.findById(req.query.id.replace(/"/g, ''), (err, result) => {
if (err) {
return res.status(500).send('Server error.')
}
res.render('edit.html', { result: result })
})
})
router.post('/students/edit', (req, res) => {
var id = req.body.id.replace(/"/g, "")
Student.findByIdAndUpdate(id, req.body, function (err) {
if (err) {
return res.status(500).send('Server error.')
}
res.redirect('/students')
})
})
router.get('/students/delete', (req, res) => {
var id = req.query.id.replace(/"/g, "")
Student.findByIdAndRemove(id, function (err) {
if (err) {
return res.status(500).send('Server error.')
}
res.redirect('/students')
})
})
module.exports = router
var fs = require('fs')
var dbPath = './db.json'
exports.findById = function (id, callback) {
fs.readFile(dbPath, 'utf8', (err, data) => {
if (err) {
return callback(err)
}
var students = JSON.parse(data).students
var result = students.find(item => {
return item.id === parseInt(id)
})
callback(null, result)
})
}
exports.find = function (callback) {
fs.readFile(dbPath, 'utf-8', (err, data) => {
if (err) {
return callback(err)
}
callback(null, JSON.parse(data).students)
})
}
exports.save = function (student, callback) {
fs.readFile(dbPath, 'utf-8', (err, data) => {
if (err) {
return callback(err)
}
var students = JSON.parse(data).students
student.id = students.length + 1
students.push(student)
var fileData = JSON.stringify({
students: students
})
fs.writeFile(dbPath, fileData, err => {
if (err) {
return callback(err)
}
callback(null)
})
})
}
exports.update = function (student, callback) {
student.id = parseInt(student.id)
fs.readFile(dbPath, 'utf-8', (err, data) => {
if (err) {
return callback(err)
}
var students = JSON.parse(data).students
var stu = students.find(function (item) {
return item.id === student.id
})
for (var key in student) {
stu[key] = student[key]
}
var updatelate = JSON.stringify({
students: students
})
fs.writeFile(dbPath, updatelate, err => {
if (err) {
return callback(err)
}
callback(null)
})
})
}
exports.delete = function (id, callback) {
id = parseInt(id)
fs.readFile(dbPath, 'utf8', (err, data) => {
if (err) {
return callback(err)
}
var students = JSON.parse(data).students
for (const item of students) {
if (item.id === id) {
students.splice(id-1,1)
}
}
var remDate = JSON.stringify({
students: students
})
fs.writeFile(dbPath, remDate, err => {
if (err) {
return callback(err)
}
callback(null)
})
})
}
var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test',{useNewUrlParser: true})
mongoose.set('useFindAndModify', false);
var Schema = mongoose.Schema
var studentSchema = new Schema({
name: {
type: String,
required: true
},
gender: {
type: Number,
enum: [0, 1],
default: 0
},
age: {
type: Number,
},
hobbies: {
type: String,
},
})
module.exports = mongoose.model('Student', studentSchema)