1. 程式人生 > >基於express的學生信息管理系統

基於express的學生信息管理系統

java bpa reject 結果 一個 sent nco search .get

基於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倉庫。

基於express的學生信息管理系統