How does mongoose implement a module to query and assign multiple routing templates?

how do I implement a module (or skeleton) in which multiple routers reference one mongodb?

simple code example:
routing:

 router.get("/", async (ctx, next)=> {
        await ctx.render("index",{})
    }
 );
 router.get("/about", async (ctx, next)=>{
        await ctx.render("about",{})
    },
 router.get("/content", async (ctx, next)=>  {
        await ctx.render("content",{})
    },

mongodb query a collection:

let mongoose = require("mongoose");
let conSchema = mongoose.Schema({

    title:{type:String},
    info:{type:String},
    keywords:{type:String},
    description:{type:String}

});

let conscModel = mongoose.model("cons",conSchema);

write the query on the route:

let mongoose = require("mongoose");
let conSchema = mongoose.Schema({

    title:{type:String},
    info:{type:String},
    keywords:{type:String},
    description:{type:String}

});

let conscModel = mongoose.model("cons",conSchema);

conscModel .find({},function(err,data) {data});
    
    
    
router.get("/", async (ctx, next)=> {
    await ctx.render("index",{})
});
router.get("/about", async (ctx, next)=>{
    await ctx.render("about",{})
});
router.get("/content", async (ctx, next)=>  {
    await ctx.render("content",{})
});
    

how to load all? This method 1 is not good:


let conscModel = mongoose.model("cons",conSchema);
let conn =  conscModel .find({},function(err,data) {data});
    
    
    
router.get("/", async (ctx, next)=> {
      await ctx.render("index",{ web:conn   })
});
router.get("/about", async (ctx, next)=>{
       await ctx.render("about",{ web:conn})
});
 router.get("/content", async (ctx, next)=>  {
       await ctx.render("content",{ web:conn})
});
    

this method 2 cannot be implemented, but if other queries cannot be written:


let conscModel = mongoose.model("cons",conSchema);


router.get("/", async (ctx, next)=> {
     await  conscModel .find({},function(err,data) {
         ctx.render("index",{ web:conn   })
        }
    
     }
});
router.get("/about", async (ctx, next)=>{
         await  conscModel .find({},function(err,data) {
         ctx.render("about",{ web:conn   })
        }
     }
});
router.get("/content", async (ctx, next)=>  {
           await  conscModel .find({},function(err,data) {
         ctx.render("content",{ web:conn   })
        }
     }
});
    

is there a better way? Or a better way to inherit?


hierarchical design
add a layer of controller between routing and model to handle business logic

//
'use strict';
import ArticleModel from '../../models/article';
class Article {
    constructor() {
      //...
    }
    async getArticleById(req, res, next) {
        //
       let article = await ArticleModel.findOne(...);
    }

    async addArticle(req, res, next) {
       //
    }
    // 
    // 
    async deleteArticle(req, res, next) {
        //
    }
    // 
    async updateArticle(req, res, next) {
        // 
        //
    }
}
export default new Article();
//
'use strict';
import express from 'express';
import Article from '../controllers/blog/article';
const router = express.Router();
router.get('/article/:id',  Article.getArticleById);
router.post('/article/add',  Article.addArticle);
router.post('/article/update',  Article.updateArticle);
router.post('/article/delete',  Article.deleteArticle);
export default router

A koa+mysql I saw earlier can also refer to it. The principle is similar to
koa-blog

.
Menu