我正在为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/middleware .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}
?