我设法使中间件在Strapi中工作。但是,我在请求中看不到正文。
在/middlewares/getEmail/index.js里面,我有
module.exports = (strapi) => {
return {
initialize: function (cb) {
strapi.app.use(async (ctx, next) => {
if (ctx.method === "POST" && ctx.url === "/email-leads") {
console.log(ctx);
}
await next();
});
},
};
};
和ctx请求日志:
request: {
method: 'POST',
url: '/email-leads',
header: {
'content-type': 'application/json',
accept: 'application/json',
'user-agent': 'PostmanRuntime/7.26.8',
'postman-token': 'xxx',
host: 'localhost:1337',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive',
'content-length': '33'
}
},
这是我在此应用程序上编写的唯一中间件。在/config/middleware.js中,我有
module.exports = {
load: {
before: ["getEmail", "responseTime", "logger", "cors", "responses", "gzip"],
order: ["parser"],
after: ["router"],
},
settings: {
getEmail: {
enabled: true,
},
},
};
我阅读了有关该koa-body / unparsed.js的内容,以阅读正文,但ctx.request中实际上没有正文。感谢帮助。
因此,我已经找到了解决方案。如果有人需要,请在此处发布。
在中config/middleware.js
,我更改了负载,将自定义中间件转换为after
数组。
load: {
before: ["responseTime", "logger", "cors", "responses", "gzip"],
after: ["parser", "router", "getEmail"],
},
ctx.request.body
如果我登录ctx
或,我仍然看不到ctx.request
。但是,如果ctx.request.body
直接使用它,则可以达到上述目的(如果负载如上所述)(或之后的自定义中间件parser
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句