簡體   English   中英

在 Strapi 中添加自定義中間件

[英]Adding Custom Middlewares in Strapi

我正在為 strapi 創建一個自定義中間件,它將響應主體更改為散列。 我需要將此中間件用於 strapi 中的所有路由,即應用程序級別。

'use strict';

/**
 * `ctx-decrypt` middleware.
 */

var crypto = require('crypto');

const algorithm = 'aes-256-cbc';

function decrypt(text,key) 
{
   let iv = Buffer.from(text.iv, 'hex');
   
   let encryptedText = Buffer.from(text.encryptedData, 'hex');
   
   let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
   
   let decrypted = decipher.update(encryptedText);
   
   decrypted = Buffer.concat([decrypted, decipher.final()]);
   
   return decrypted.toString();
}

function hash(data)
{
  var hash = crypto.createHash('sha256', data);

  return hash;
}

module.exports = (config,{env},{strapi}) => 
{
  return 
  {
    initialize() 
    {
      strapi.app.use(async (ctx, next) => 
      {

        strapi.log.info('In ctx-decrypt middleware.');

        var ENC_KEY = process.env.ENC_KEY;
        
        var requestBody = ctx.request.body;
        //var responseBody = ctx.body;

        var dc = decrypt(requestBody,ENC_KEY);

        if(!dc)
        {
           throw new Error('Invalid request body!');

           ctx.response.set(hash("5u5234803riqifn"));

           console.log(ctx.body)
           
           await next();
        }
        
        else
        {
           ctx.response.set(hash("5u5234803riqifn"));

           console.log(ctx.body)
           
           await next();
        }

      });

    }

  };

};

現在我關注了他們最新的文檔,但 strapi 在使用該中間件時總是會出現一些錯誤。 我跑了

npx strapi generate 

在項目的根目錄生成中間件。 然后我跑了

npx strapi middlewares:list

獲取所有中間件名稱,然后像這樣在 ./config/middlewares.js 中添加我的“global::ctx-decrypt”中間件名稱

module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::cors',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
  'global::ctx-decrypt',
];

它給我這樣的錯誤

0|super-fu | [2022-12-23 02:48:51.181] debug: ⛔️ Server wasn't able to start properly.
0|super-fu | [2022-12-23 02:48:51.183] error: Middleware "global::ctx-decrypt": Cannot destructure property 'strapi' of 'undefined' as it is undefined.
0|super-fu | Error: Middleware "global::ctx-decrypt": Cannot destructure property 'strapi' of 'undefined' as it is undefined.
0|super-fu |     at instantiateMiddleware (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/services/server/middleware.js:12:11)
0|super-fu |     at resolveMiddlewares (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/services/server/middleware.js:56:18)
0|super-fu |     at registerApplicationMiddlewares (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/services/server/register-middlewares.js:66:29)
0|super-fu |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
0|super-fu |     at async Object.initMiddlewares (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/services/server/index.js:99:7)
0|super-fu |     at async Strapi.bootstrap (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/Strapi.js:414:5)
0|super-fu |     at async Strapi.load (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/Strapi.js:426:5)
0|super-fu |     at async Strapi.start (/home/blackhole/nodejs/super-funnels/super-funnels-backend/node_modules/@strapi/strapi/lib/Strapi.js:169:9)
0|super-fu | 
0|super-fu | > super-funnels-backend@0.1.0 start
0|super-fu | > strapi start

我該如何解決這個問題並使用這個中間件。

根據文檔,中間件的正確函數定義如下:

module.exports = (config, { strapi })=> {
  return (context, next) => {};
};

沒有第三個參數所以也許試試{env, strapi}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM