mongoose 如何实现一个模块查询赋值多个路由模板?

Mini1314
  • 98

如何实现多个路由器引用1个mongodb的模块(或者骨架)?

简单代码例子:
路由:

 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查询一个集合:

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);

把查询写在路由上:

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',{})
});
    

如何实现都加载?这个方法1是不行:


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})
});
    

这个方法2是不行能实现,但是 如果其他查询是没办法写了:


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   })
        }
     }
});
    

有更好办法吗?或者更好的继承办法?

回复
阅读 1.6k
1 个回答
臧瑟
  • 401

分层设计
路由和模型之间加一层控制器来处理业务逻辑

//以文章为例,文章控制器
'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

我之前看到的一个koa+mysql写的也可以参考下,原理类似
koa-blog

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏