基於express實現三層架構開發。下圖為學生管理系統開發
。一般為相應的HTML發送Ajax請求。即為從sms.html 發送Ajax請求給students.js

一:划分為1:表示層,主要為接受用戶發送的請求,以及數據的返回,為客戶端提供應用程序的訪問.2:業務邏輯層:主要負責對數據的操作,也就是把一些數據層的操作進行組合。3:數據訪問層。主要時對數據進行操作
二:基於express建立的三層結構,一般目錄如下。,public(靜態頁面html書寫處),routes(里面的stuendts.js接受靜態頁面的請求,及返回數據),service(里面的stuendtsService.js 對routes里面的stuendts.js傳來的數據進行操作,並返回結果),dao(數據層)
注:dao層,作為數據處理層,一般也分為三部分,參見最后一圖

dao層,作為數據處理層,一般也分為三部分。①(db.js),直接和數據庫相連。②在dao文件夾里面建立子文件見models,在其下面建立數據庫的骨架,並根據骨架創立模型。(studentsModel.js)③(studentsDao.js),直接對數據進行操作的界面,接收service文件夾下studentsService.js 傳來的數據,根據傳來的數據,對數據庫里面的數據進行增刪查改的操作,並返回結果給service文件夾。

三詳細流程和實現代碼。
1:public下的靜態頁面
//發送Ajax
Ajax({
url:"./students/getStudents",
data:,
success(){
}
})
2:routes文件夾下的students.js//接收頁面發來的信息,並反饋
//引文件
var express = require('express');
var router = express.Router();
const studentsService = require("../service/studentsService");
//具體操作如下操作
router.get('/getStudents', async function (req, res, next) {
const pager = req.query;
const data = await studentsService.getStudents(pager);
res.send(data);
});
module.exports= router;
3:service文件夾下的studentsService.js//
const studentsDao = require("../dao/studentsDao"); //引入
const getStudents = async (pager) => {
let data = await studentsDao.getStudents(pager)
return data
}
module.exports = { getStudents }//暴露
4: studentsDao.js//對數據庫的數據進行操作
const mongoose = require("mongoose");//引入mongoose
const studentsModel = mongoose.model("students");
const getStudent = async ( )=>{
const data = await studentsModel.find();
return data;
}
module.exports = { getStudents }//暴露
5:db.js//連接數據庫
const mongoose = require("mongoose");//引入mongoose
mongoose.connect("mongodb://localhost/xxxxx",{useNewUrlParser:true,useUnifiedTopology:true});
mongoose.connection.on("connected",function(){
console.log("mongoose已經成功鏈接上了xxxxx");
})
require("./models/studentsModel")//引入文件studentsModel.js
6:studentsModel.js//創建數據模板
const mongoose = require("mongoose");//引入mongoose
const studentsSchema = new mongoose.Schema({//創建骨架
name: String,
age:Number,
gender:String,
grade:Number,
addr:String,
phone:String,
}, {versionKey: false});
const studentsModel = mongoose.model('students', studentsSchema,"students");//根據骨架創建模版