假设所有的数据库读取,http api 接口请求都为一个中间件,将中间件当做插件,插入需要获取数据的位置。
api.js
module.exports = async (ctx, next) => { ctx.share_data.api_data = await axios.get('/api'); await next(); };
db.js
module.exports = async (ctx, next) => { ctx.share_data.db_data = await mysql_query('SELECT XXX').catch(() => {}); await next(); };
串联
app.js
const api = require('api.js'); const db = require('db.js'); app.get('/get-api', api, (ctx) => ctx.body = ctx.share_data); app.get('/get-db', db, (ctx) => ctx.body = ctx.share_data); app.get('/get-api-and-db', api, db, (ctx) => ctx.body = ctx.share_data);
看着挺和谐,但是如果有多个数据中间件串联则会导致接口的响应时间为所有中间件的总和。
并发
可义一个 compose 函数,需要并发的中间件包装起来
super-compose.js
module.exports = (middleware = []) => { if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!'); for (const fn of middleware) { if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!'); } return async (context = {}, next = f => f) => { await Promise.all( middleware.map(middleware => { return new Promise((rs, rj) => { middleware(context, () => Promise.resolve()) .then(rs) .catch(rj); }); }), ); await next(); }; };
app.js
const api = require('api.js'); const db = require('db.js'); const superCompose = require('super-compose.js'); app.get('/get-api-and-db', superCompose([api, db]), (ctx) => ctx.body = ctx.share_data);
依赖关系
看着貌似解决了,但如何处理具有上下文依赖的情况呢?例如 api_1 依赖 api 的数据。
改下 api.js,加上缓存校验。处理可能被多次compose的重复接口调用
module.exports = async (ctx, next) => { if (ctx.share_data.api_data) { return await next(); } ctx.share_data.api_data = await axios.get('/api'); await next(); };
api-1.js
const api = require('api.js'); module.exports = compose([ api, async (ctx, next) => { const { api_data: { api_data: { id = 0 } = {} } = {} } = ctx; if (id < 0) { await next(); } else { ctx.api_data.api_1_data = await axios.get('/api', { params: { id } }); } await next(); }, ])
app.js
const api_1 = require('api_1.js'); const db = require('db.js'); const superCompose = require('super-compose.js'); app.get('/get-api-and-db', superCompose([api_1, db]), (ctx) => ctx.body = ctx.share_data);
跳过中间件
有时候,需要根据特定的条件,绕过某些接口调用
改造下 api.js,通过加入过滤列表
module.exports = async (ctx, next) => { const { break_list = [] } = ctx; if (break_list.includes('api_data')) { // 可能会误伤其他组合引用该中间件的情况。 // 如可能会误伤,可加上。 // ctx.break_list = break_list.filter(v => v !== 'api_data') return await next(); } else { ctx.share_data.api_data = await axios.get('/api'); } await next(); }
app.js
const api = require('api.js'); const db = require('db.js'); const superCompose = require('super-compose.js'); app.get( '/get-api-and-db', async (ctx, next) => { ctx.break_list = ['api_data']; await next(); }, superCompose([api, db]), ctx => (ctx.body = ctx.share_data) );
数据合并处理
结合 super-compose 与 koa-compose 将所有需要的中间件组合起来,在写一个针对页面的controller
const api = require('api.js'); const db = require('db.js'); const superCompose = require('super-compose.js'); const compost = rquire('koa-compose') const babala = compose([ superCompose([api, db]), async (ctx, next) => { const { share_data: { api_data: { id = 0 } = {}, db_data: { title } = {} } = {}, } = ctx; ctx.body = { id, title }; // OR // ctx.share_data.babala = {} }, ]); app.get( '/get-api-and-db', babala );
结尾
解决经常出现的一个函数内大量的接口、逻辑操作,超长的上下文逻辑。
app.get('/api', async ctx => { const api_1 = await axios.get('/api_1'); await api_2 = await axios.get('/api_2'); // ... // ... // 这里有一百行 // ... const [api_3, api_4] = await new Promise.all([axios.get('/api_3'), axios.get('/api_4')]); // ... // ... // 这里有几百行 // ... ctx.body = {}; });
以上就是koa2 数据api中间件设计模型的实现方法的详细内容,更多关于koa2 中间件设计模型的资料请关注其它相关文章!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 陈慧琳.2002-闪亮每一天新歌+精选2CD【正东】【WAV+CUE】
- 袁培华.1992-有缘【歌林】【WAV+CUE】
- 王瑞霞.1996-是我太软心【名冠】【WAV+CUE】
- 《谭维维 30首经典流行歌曲精选辑 2CD》[WAV/分轨][1.1GB]
- 《赵传 出道三十年全新大碟 你过得还好吗》[WAV/分轨][460MB]
- 《发烧四大天王 磁性男声HiFi发烧碟 2CD》[WAV/分轨][1GB]
- 黑神话悟空上品鼠司空精魄获取方法一览|上品鼠司空精魄收集攻略
- 《真三国无双:起源》新情报将于9月26日公开,实机演示亮相!
- 黑神话悟空上品鼠禁卫精魄获取方法一览|上品鼠禁卫精魄收集攻略
- 优美的旋律至真的情怀《品茶轻音乐·极品典藏Vol.1CD1》[WAV+CUE]
- 曾嵘《峥嵘岁月DSD(试音碟)》WAV+CUE
- 小提琴天碟AlfredoCampoli坎波利-贝多芬D大调小提琴协奏曲与普罗米修斯作品选FLAC
- 博主称本周游戏业内动荡无比:高价PS5 Pro、Xbox裁员等
- 米哈游也搞AI?米哈游AI大模型Glossa完成备案
- G胖变G瘦后:玩家担心其健康状况:如果他出事 游戏业将发生巨变