3層アーキテクチャの開発ベースの急行を達成するために。以下の図は、学生管理システムの開発を示してい
。通常、アヤックスは、適切なHTMLに要求します。students.js sms.htmlアヤックスへの要求から送信されていること
:主にユーザーによって送信された要求を受け入れるために、プレゼンテーション層、およびデータは、クライアント・アプリケーションへのアクセスを提供0.2、返されます:1が分割され、ビジネスロジック層:データの操作のための主に担当し、すなわち、いくつかのデータ層を複合動作。3:データアクセス層。場合は、メインデータの操作
2:一般的なディレクトリとして、急行に基づいて3層構造を確立します。、公共の(静的なHTMLの書き込みでページ)、ルート(stuendts.js内のリクエスト静的なページ、および戻り値のデータを受け入れる)、 - 操作からstuendts.jsデータ内のルートの内部サービス(stuendtsService.js、およびリターン結果)、DAO(データ層)
注:一般に、3つの部分、最後の参照図に分割DAO層、データ処理層、。
DAO層、データ処理層は、一般的に3つの部分に分かれ。①(db.js)は、データベースに直接接続されています。②スケルトンに基づいてスケルトンの下のデータベース、およびモデルの作成を確立し、モデルを見るサブフォルダ内のDAOフォルダを確立します。(StudentsModel.js)③(studentsDao.js)、送信されたデータ、データベース検索動作欠失改革内部データに従って送信サービスデータフォルダstudentsService.jsを受信するダイレクトデータインタフェース動作、サービスフォルダに結果を返します。
三つは、プロセスと実装コードを詳しく説明しました。
1:公共の下で静的なページ
//アヤックスを送ります
AJAX({
URL: "./学生/ getStudents"、
データ:、
成功(){
}
})
2:フォルダ//情報ページを受信students.jsルートが送信され、フィードバック
//インデックスファイル
VaRは発現=(「発現」)を必要とします。
VARルータ= 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");//根据骨架创建模版