nodejs+express文件操作实现小型学生管理系统(增删改查)-纯享版
Posted 勇敢*牛牛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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, res)
Student.find(function (err, students)
if (err)
return res.status(500).send('Server error.');
res.render('index.html',
students: students
);
)
)
router.get('/students/new', function (req, res)
res.render('new.html');
)
router.post('/students/new', function (req, res)
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, res)
Student.findId(parseInt(req.query.id), function (err, student)
if (err)
return res.status(500).send('Server error.');
res.render('edit.html',
student: student
)
)
)
router.post('/students/edit', function (req, res)
Student.updataByid(req.body, function (err)
if (err)
return res.status(500).send('Server error.');
)
res.redirect('/students')
)
router.get('/students/delete', function (req, res)
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, data)
if (err)
return callback(err)
callback(null, JSON.parse(data).student)
)
exports.save = function (Nstudent, callback)
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err, data)
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, callback)
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err, data)
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, callback)
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err, data)
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, callback)
fs.readFile(path.join(__dirname, dbPath), 'utf8', function (err, data)
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);
)
)
以上是关于nodejs+express文件操作实现小型学生管理系统(增删改查)-纯享版的主要内容,如果未能解决你的问题,请参考以下文章
NodeJS+express+MySQL实现博客系统(入门级)
nodejs--express的中间件multer实现图片文件上传--XUEBIG