亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

koa2 數(shù)據(jù)api中間件設(shè)計模型的實現(xiàn)方法

 更新時間:2020年07月13日 09:22:11   作者:誤人子弟  
這篇文章主要介紹了koa2 數(shù)據(jù)api中間件設(shè)計模型的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

假設(shè)所有的數(shù)據(jù)庫讀取,http api 接口請求都為一個中間件,將中間件當(dāng)做插件,插入需要獲取數(shù)據(jù)的位置。

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

串聯(lián)

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

看著挺和諧,但是如果有多個數(shù)據(jù)中間件串聯(lián)則會導(dǎo)致接口的響應(yīng)時間為所有中間件的總和。

并發(fā)

可義一個 compose 函數(shù),需要并發(fā)的中間件包裝起來

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

依賴關(guān)系

看著貌似解決了,但如何處理具有上下文依賴的情況呢?例如 api_1 依賴 api 的數(shù)據(jù)。
改下 api.js,加上緩存校驗。處理可能被多次compose的重復(fù)接口調(diào)用

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

跳過中間件

有時候,需要根據(jù)特定的條件,繞過某些接口調(diào)用

改造下 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)
);

數(shù)據(jù)合并處理

結(jié)合 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
);

結(jié)尾

解決經(jīng)常出現(xiàn)的一個函數(shù)內(nèi)大量的接口、邏輯操作,超長的上下文邏輯。

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 數(shù)據(jù)api中間件設(shè)計模型的實現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于koa2 中間件設(shè)計模型的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • node進程管理工具PM2用法詳解

    node進程管理工具PM2用法詳解

    本文詳細(xì)講解了node進程管理工具PM2的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 使用 Node.js 模擬滑動拼圖驗證碼操作的示例代碼

    使用 Node.js 模擬滑動拼圖驗證碼操作的示例代碼

    本篇文章主要介紹了使用 Node.js 模擬滑動驗證碼操作的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Node.js處理I/O數(shù)據(jù)之使用Buffer模塊緩沖數(shù)據(jù)

    Node.js處理I/O數(shù)據(jù)之使用Buffer模塊緩沖數(shù)據(jù)

    這篇文章介紹了Node.js使用Buffer模塊緩沖數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • node.js文件的壓縮解壓問題

    node.js文件的壓縮解壓問題

    這篇文章主要介紹了node.js文件的壓縮解壓問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Node.js 多線程完全指南總結(jié)

    Node.js 多線程完全指南總結(jié)

    這篇文章主要介紹了Node.js 多線程完全指南總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Node.js使用bcrypt-pbkdf實現(xiàn)密碼加密

    Node.js使用bcrypt-pbkdf實現(xiàn)密碼加密

    在這個數(shù)字時代,保護用戶密碼的重要性不言而喻,作為一名資深的前端開發(fā)工程師和技術(shù)博客作者,今天我將帶你詳細(xì)了解如何在 Node.js 環(huán)境中利用 bcrypt-pbkdf 模塊進行密碼的哈希處理,確保你的應(yīng)用安全性得到有效提升,需要的朋友可以參考下
    2024-05-05
  • 淺談NodeJS中require路徑問題

    淺談NodeJS中require路徑問題

    學(xué)習(xí)Nodejs也是出于對這個新產(chǎn)物的好奇,而且有兩個重要項目也需要他的支持,所以要好好學(xué)學(xué)這個新語種。在nodejs中,模塊大概可以分為核心模塊和文件模塊。核心模塊是被編譯成二進制代碼,引用的時候只需require表示符即可
    2015-05-05
  • node使用mysql獲取數(shù)據(jù)庫數(shù)據(jù)中文亂碼問題的解決

    node使用mysql獲取數(shù)據(jù)庫數(shù)據(jù)中文亂碼問題的解決

    這篇文章主要介紹了node使用mysql獲取數(shù)據(jù)庫數(shù)據(jù)中文亂碼問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 玩轉(zhuǎn)Koa之核心原理分析

    玩轉(zhuǎn)Koa之核心原理分析

    這篇文章主要介紹了玩轉(zhuǎn)Koa之核心原理分析,本文從封裝創(chuàng)建應(yīng)用程序函數(shù)、擴展res和req、中間件實現(xiàn)原理、異常處理的等這幾個方面來介紹,感興趣的可以了解一下
    2018-12-12
  • nodeJS與MySQL實現(xiàn)分頁數(shù)據(jù)以及倒序數(shù)據(jù)

    nodeJS與MySQL實現(xiàn)分頁數(shù)據(jù)以及倒序數(shù)據(jù)

    這篇文章主要介紹了nodeJS與MySQL實現(xiàn)分頁數(shù)據(jù)以及倒序數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評論