学籍管理系统

效果展示:

1.首页

2.班级管理

3.添加学生

 

 4.学生列表

 


代码展示: 

1.npm初始化与模块安装

npm init

npm install express

npm install ejs

npm install mongoose

2.服务器文件(server.js)

var express = require("express");
var app = express();
// use代表中间件:app继承了静态文件夹功能
app.use(express.static("www"));
// set告诉app当用到ejs模板的时候去views文件夹查找
app.set("view engine", "ejs");
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
    extended: false
}));
// 构建数据库
var Grade = require("./bin/DAO/GradeDAO.js");
var Student = require("./bin/DAO/StudentDAO.js");
app.listen(3000, function () {
    console.log("Server-Running...");
});

// 班级管理的接口
app.get("/grade", function (req, res) {
    // 查询
    Grade.find().exec(function (err, data) {
        res.render("grade.ejs", {
            data
        });
    });
});

app.post("/api/grade/add", function (req, res) {
    console.log("???" + req.body.major);
    // 准备对象
    var g = new Grade({
        major: req.body.major,
        name: req.body.name,
        room: req.body.room,
        teacher: req.body.teacher
    });


    g.save(function (err) {
        if (!err) {
            res.redirect("/grade");
        }
    });
});

// 学生接口
// 班级管理的接口
app.get("/student/add", function (req, res) {
    // 查询
    Grade.find().exec(function (err, data) {
        res.render("student.ejs", {
            data
        });
    });
});
// 添加
app.post("/api/student/add", function (req, res) {
    var s = new Student(req.body);
    console.log(s);

    s.save(function (err) {
        if (!err) {
            res.redirect("/student/add");
        }
    });
});

// 学生列表
app.get("/student-List", function (req, res) {
    // populate方法,用于将查询结果中某一列替换成该列id所关联的表对象
    Student.find().populate("grade").exec(function (err, data) {
        res.render("student-list.ejs", {
            data
        });
    });
});

3.数据库文件(DAO)

(1)Connection.js

var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017/stuMsg-list", {
    useNewUrlParser: true
}, function (err) {
    if (!err) {
        console.log("MongoDB-Running...");
    }
});
module.exports = mongoose;

(2)GradeDAO.js

var mongoose = require("./Connection.js");
var gradeSchema = new mongoose.Schema({
    major: String, //专业
    room: String, //教室
    teacher: String, //老师
    name: String //班级名称
});
var grade = mongoose.model("grade", gradeSchema);
module.exports = grade;

(3)StudentDAO.js

var mongoose = require("./Connection.js");
var studentSchema = new mongoose.Schema({
    name: String,
    // 集合:对象(哈希表,字典)
    // 在表结构中,除了数字,字符串,数组....还可以声明object
    // 一般声明ObjectId都是为了关联外部表使用
    grade: {
        /*
           type表示这一列是什么类型
           mongoose.Schema.Types.ObjectId 描述当前这一列的
           关联到其他表的外键
        */
        type: mongoose.Schema.Types.ObjectId,
        //  关联到哪一个表上
        ref: "grade"
    }
});
var student = mongoose.model("student", studentSchema);
module.exports = student;

ejs模板和静态文件省略....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值