如何在Moleculer中获取请求正文

时间:2019-11-21 20:58:34

标签: javascript rest microservices backend moleculer

根据API文档,要从POST请求接收json作为formData,必须使用body-parser。我已经在网关服务中声明了它,但是我仍然无法在操作中收到formData。

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 }
    },
 },
};

在操作service.insertUser操作中,我应该收到 req.body为ctx.params,但始终为空

我的users.service.js

    actions: {
    insertUser: {
        handler(ctx) {
            this.logger.info("posting", ctx.params); // -> prints {} instead of the formData
        }
    }

1 个答案:

答案 0 :(得分:0)

您是否尝试过提供参数

(df.sum(axis=1)>df.shape[1]/2)+0

img