Nodejs, three-tier architecture

To achieve three-tier architecture development based express. The figure below shows the development of the student management system

. Usually Ajax requests to the appropriate HTML. That is transmitted from the request to students.js sms.html Ajax

A: 1 is divided into: the presentation layer, mainly to accept a request sent by the user, and the data is returned, .2 providing access for the client application: business logic layer: mainly responsible for the operation of the data, i.e. some data layer the combined operation. 3: data access layer. When the operation of the main data

Two: establish a three-tier structure based on express, as a general directory. , Public (static html page at write), routes (inside stuendts.js accept requests static page, and return data),-Service (stuendtsService.js inside of routes inside stuendts.js data from the operation, and return result), dao (data layer)

NOTE: dao layer, the data processing layer, generally divided into three parts, a last see FIG.

dao layer, the data processing layer, generally divided into three parts. ① (db.js), directly connected to the database. ② establish dao folder inside subfolders see models, establish a database of the skeleton underneath, and the creation of the model based on the skeleton. (StudentsModel.js) ③ (studentsDao.js), direct data interface operation receiving studentsService.js folder service data transmitted, according to the transmitted data, the data inside the database search operation deletions reform and returns the result to the service folder.

Three detailed processes and implementation code.

1: static pages under public

// send Ajax

Ajax({

    url:"./students/getStudents",

    data:,

   success(){

}

})

2: students.js routes folder // receiving information pages sent, and feedback

// index file

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");//根据骨架创建模版
 

Guess you like

Origin www.cnblogs.com/yt0817/p/12115864.html