moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
292 stars 118 forks source link

Uploading File along with metadata in moleculer framework #307

Open vishalgilbilemoodys opened 2 years ago

vishalgilbilemoodys commented 2 years ago

Hello, I tried following the moleculer framework documentation for uploading the file as multipart, by making change in ApiGateway route & setting the aliases with the rest route as mutlipart:service.action. Below is the snap of the ApiGateway settings

 {
                    path: "/api2",
                    authorization: true, //run method `authorize` before calling handler
                    // Use bodyparser module
                    bodyParsers: {
                        json: false,
                        urlencoded: false
                    },
                    mappingPolicy: "all",
                    aliases: {
                        // File upload from HTML multipart form
                        "POST /v2.dataviewer.upload": "multipart:v2.dataviewer.upload"
                    },
                    // Route level busboy config.
                    // More info: https://github.com/mscdex/busboy#busboy-methods
                    busboyConfig: {
                        limits: { files: 1 }
                        // Can be defined limit event handlers
                        // `onPartsLimit`, `onFilesLimit` or `onFieldsLimit`
                    },
                    // Call before `broker.call`
                    onBeforeCall(ctx, route, req) {
                        this.addExtraMetaData_beforeCall(ctx, route, req);
                    },
                    // Call after `broker.call` and before send back the response
                    onAfterCall(ctx, route, req, res, data) {
                        this.addExtraMetaData_afterCall(
                            ctx,
                            route,
                            req,
                            res,
                            data
                        );

                        //do stuff
                        let urlSplit = req.url.split('/'),
                            services = '/' + urlSplit[1] + '/' + urlSplit[2];
                        // Increment this metric (api.usage.total) counter by 1 everytime a particular api is called.
                        // This metric is used in grafana to see how many times this api is called by a individual user
                        this.broker.metrics.increment("api.usage.total", {
                            endpoint: req.url,
                            services: services,
                            login: ctx.meta.user.login,
                            version: this.version
                        }, 1);
                        //!important, has to return data after doing stuff
                        return data;
                    },
                    onError(req, res, err) {
                        // Increment this metric (api.error.total) counter by 1 everytime there is an error in API call.
                        // This metric is used in grafana to see how many times API failed for user level
                        this.formatAndSendError(res, err);
                        this.broker.metrics.increment("api.error.total", {
                            endpoint: req.url,
                            login: req.$ctx.meta.user ? req.$ctx.meta.user.login : "@unauthorized",
                            version: this.version
                        }, 1);
                    }
                }

But still in the service layer I'm getting undefined in the service Action method

@Action({
        cache: false,
        params: {
        }
    })
    async upload(ctx) {
        let request = ctx.params;
        request.user = ctx.meta.user;
        console.log('CTX', ctx);
        console.log('Params', ctx.params);
        console.log('ctx.meta.$multipart', ctx.meta.$multipart); //undefined
        console.log("Received upload $params:", ctx.meta.$params); //undefined
        return null;
    }

Any help is highly appreciated.

dolly1997 commented 11 months ago

Hi @vishalgilbilemoodys is this issue resolved? i am facing the same issue!