繁体   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