nodejs+express文件操作实现小型学生管理系统(增删改查)-纯享版
作者:快盘下载 人气:实现文件数据更新功能
用来放置一些不希望被用户看见,但是需要被提交到服务端的数据
因为读取的文件信息,然后他的类型是字符串,切记
…………………………
student.id = parseInt(student.id);
var stu = students.find(function (item) {
return item.id === student.id
})
app.js
var express = require('express');
var path = require('path');
var router = require(path.join(__dirname, './router'));
// 引包
var bodyParser = require('body-parser');
var server = express();
server.engine('html', require('express-art-template'));
server.set('views', path.join(__dirname, 'views'));
server.use('/node_modules/', express.static(path.join(__dirname, './node_modules/')));
server.use('/public/', express.static(path.join(__dirname, './public/')));
server.use(bodyParser.urlencoded({ extended: false }))
.use(bodyParser.json())
server.use(router);
server.listen(3000, function () {
console.log('Run Server 3000……');
})
router.js
// 路由模块设计集合
var fs = require('fs');
var path = require('path');
var Student = require('./student');
var express = require('express');
var router = express.Router();
router.get('/students', function (req,) {
Student.find(function (err,) {
if (err) {
return res.status(500).send('Server error.');
}
res.render('index.html', {
students: students
});
})
})
router.get('/students/new', function (req,) {
res.render('new.html');
})
router.post('/students/new', function (req,) {
var student = req.body;
Student.save(student, function (err) {
if (err) {
return res.status(500).send('Server error.');
}
res.redirect('/students');
})
})
router.get('/students/edit', function (req,) {
Student.findId(parseInt(req.query.id), function (err,) {
if (err) {
return res.status(500).send('Server error.');
}
res.render('edit.html', {
student: student
})
})
})
router.post('/students/edit', function (req,) {
Student.updataByid(req.body, function (err) {
if (err) {
return res.status(500).send('Server error.');
}
})
res.redirect('/students')
})
router.get('/students/delete', function (req,) {
Student.deleteByid(req.query.id, function (err) {
if (err) {
return res.status(500).send('Server error.');
}
res.redirect('/students')
})
})
module.exports = router;
student.js
var fs = require('fs');
var path = require('path');
var dbPath = './db.json';
exports.find = function (callback) {
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err,) {
if (err) {
return callback(err)
}
callback(null, JSON.parse(data).student)
})
}
exports.save = function (Nstudent,) {
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err,) {
if (err) {
return callback(err)
}
var students = JSON.parse(data).student;
Nstudent.id = students[students.length - 1].id + 1;
students.push(Nstudent);
var fileData = JSON.stringify({
student: students
})
fs.writeFile(path.join(__dirname, dbPath), fileData, function (err) {
if (err) {
return callback(err)
}
callback(null);
});
})
}
exports.updataByid = function (student,) {
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err,) {
if (err) {
return callback(err)
}
var students = JSON.parse(data).student;
student.id = parseInt(student.id);
var stu = students.find(function (item) {
return item.id === student.id
})
for (var key in student) {
stu[key] = student[key];
}
var fileData = JSON.stringify({
student: students
})
fs.writeFile(path.join(__dirname, dbPath), fileData, function (err) {
if (err) {
return callback(err)
}
callback(null);
})
})
}
exports.findId = function (id,) {
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err,) {
if (err) {
return callback(err)
}
var students = JSON.parse(data).student;
var ret = students.find(function (item) {
return item.id === parseInt(id)
})
callback(null, ret);
})
}
exports.deleteByid = function (id,) {
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err,) {
if (err) {
return callback(err)
}
var students = JSON.parse(data).student;
var deleteID = students.findIndex(function (item) {
return item.id === parseInt(id);
})
students.splice(deleteID, 1);
var fileData = JSON.stringify({
student: students
})
fs.writeFile(path.join(__dirname, dbPath), fileData, function (err) {
if (err) {
return callback(err)
}
callback(null);
})
})
}
加载全部内容