基于express的学生信息管理系统
项目地址
项目简介
简单的学生信息管理系统,后台基于express,数据库mysql,前端用到了一点vue,这次的团队项目我主要是负责后端的部分,所以这里对于前端代码不再赘述,只介绍后端部分以及一点点前端的js代码。
后端部分
//server.js
var bparse= require('body-parser')
app.use(bparse.urlencoded({extended:true}))
这里一定要引用body-parser,用来解析express的post方法接收的req.body。
以下是完整的server.js代码
//server.js
var db = require('./databaseServer/database');
var query_0 = require('./databaseServer/query');
var adddb = require('./databaseServer/addData');
var bparse= require('body-parser')
db.connection.connect();
var express = require('express');
var app = express();
var query_sql = query_0.query;
var add_sql = adddb.addDB;
app.use(bparse.urlencoded({extended:true}))
app.use(express.static('public'));
app.get('/index.html',(req,res)=>{
res.sendFile(__dirname+'/'+'index.html')
})
app.get('/students',(req,res)=>{
var querysentence = 'select * from student1;';
add_sql(querysentence).then((ans)=>{
res.end(JSON.stringify(ans))
});
})
app.post('/add_stu',(req,res)=>{//For home page's router;
var stu_info ={
'add_info': req.body
}
console.log(stu_info.add_info)
var querysentence = 'insert into student1'
var values = "('"
var keys = '('
for(var key in stu_info.add_info) {
keys += key
values += stu_info.add_info[key]
if(key === 'stime'){
keys += ')'
values += "')"
} else {
keys += ','
values += "','"
}
}
querysentence += keys + ' values' + values + ';'
var querysentence_2 = 'select * from student1'
console.log(querysentence)
add_sql(querysentence).then(()=>{
add_sql(querysentence_2).then((ans)=>{
res.end(JSON.stringify(ans))
})
});
})
app.post('/search_stu',(req,res)=>{
var querysentence="select * from student1 where sname='"+req.body+"':"
console.log(querysentence)
add_sql(querysentence).then((ans)=>{
console.log(ans)
res.end(JSON.stringify(ans))
})
})
app.post('/delete_stu',(req,res)=>{
var snum = req.body;
var snum_0;
for(var key in snum){
snum_0=key;
}
console.log(snum_0)
var querysentence_2 = 'select * from student1'
var querysentence = "delete from student1 where snum='"+snum_0+"';";
console.log(querysentence)
add_sql(querysentence).then((ans)=>{
add_sql(querysentence_2).then((ans)=>{
res.end(JSON.stringify(ans))
})
})
})
app.post('/change_stu',(req,res)=>{
var sname_0 = req.body
var querysentence_2 = 'select * from student1'
var querysentence = "update student1 set sname='"+sname_0.sname+"',snum='"+sname_0.snum+"',sschool='"+sname_0.sschool+"',sclass='"+sname_0.sclass+"',sgender='"+sname_0.sgender+"',smajor='"+sname_0.smajor+"' where snum='"+sname_0.searchnum+"';"
console.log(querysentence)
add_sql(querysentence).then((ans)=>{
add_sql(querysentence_2).then((ans)=>{
res.end(JSON.stringify(ans))
})
})
})
const hostname = 'localhost'
app.listen(3000,hostname,()=>{
console.log("connecting to port 3000...")
})
这里并没有用Sequelize,想重新温习以下mysql语句,所以整个后端都是拼接成mysql语句再与数据库进行数据操作,当然用Sequelize 会简单很多。
//database.js
var mysql = require('mysql')
exports.connection = mysql.createConnection({
host: 'localhost'
port: '3306',
user: 'root',
password: '',
database: ''
})
数据库配置的node文件
//addData.js
var mysql = require('mysql')
var db = require('./database');
var addData = (dbSentence)=>{
return new Promise((resolve,reject)=>{
db.connection.query(dbSentence,(er,res,feilds)=>{
if(er) reject(er);
resolve(res);
//console('have been inserted.')
})
})
}
exports.addDB = addData;
数据库查询语句传入数据库的node文件,这里面返回一个promise,传出查询到的数据库结果在server.js里面引用。
前端代码在此不再赘述,详细代码可以看git仓库。