[英]How do I get request body in Moleculer
According to the API documentation, to receive json as formData from a POST request, one must use body-parser.根据 API 文档,要从 POST 请求中接收 json 作为 formData,必须使用 body-parser。 I have declared it in the gateway service but I can still not receive the formData in my action.
我已经在网关服务中声明了它,但我仍然无法在我的操作中收到 formData。
api.service.js api.service.js
module.exports = {
name: "api",
mixins: [ApiGateway],
settings: {
port: process.env.PORT || 3000,
routes: [{
path: "/api",
aliases: {
"POST users": "users.insertUser",
},
//The API Documentation recomends using the body-parser here
bodyParsers: {
json: true,
urlencoded: { extended: true }
},
}],
// In some example they also set the body-parser here
bodyParsers: {
json: true,
urlencoded: { extended: true }
},
},
};
In the actions service.insertUser action I am supposed to receive the req.body as ctx.params, however it is always empty在操作 service.insertUser 操作中,我应该将 req.body 作为 ctx.params 接收,但它始终为空
My users.service.js我的 users.service.js
actions: {
insertUser: {
handler(ctx) {
this.logger.info("posting", ctx.params); // -> prints {} instead of the formData
}
}
send post data with content-type:application/json使用 content-type:application/json 发送帖子数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.