moleculerjs / moleculer-web

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

file streaming via alias has an empty $params object #211

Closed fjeglinski closed 3 years ago

fjeglinski commented 3 years ago

Hi there,

currently we have a problem with file streaming when we using a NATS server for service communication. When we set up the application on our local machine everything works as expected.

The problem is that while using NATS the $params object is empty, although we defined this param as part of an alias. Like described here: https://moleculer.services/docs/0.14/moleculer-web.html#File-upload-aliases

And due to the empty $params object the validation for the stream receiving function fails.

The call of

'POST /event/:id': 'stream:event.addFile',

is correct because the code is invoked correctly on localhost.

This is our API Gate Way Code:

                             {
                            path: '/files',
                            authorization:  true,
                            autoAliases: true,
                            mergeParams: true,
                            mappingPolicy: 'restrict',
                            bodyParsers: {
                                json: false,
                                urlencoded: false
                            },
                            aliases: {
                                'POST /event/:id': 'stream:event.addFile',
                            }
                        },

And this is the corresponding addFile Action:

      addFile: {
                    params: {
                        id: 'string'
                    },
                    async handler(ctx: Context<{ $params: { id: string }, headers: any }, { $statusCode: number }>): Promise<void> {
                    ...
                    }

Every help is appreciated =). Thanks guys!

icebob commented 3 years ago

Please create a repro repo.

fjeglinski commented 3 years ago

Of course: https://repl.it/repls/AlarmingGiganticComputerscience#index.js

intech commented 3 years ago

this is my repl =) reference: https://github.com/moleculerjs/moleculer-web/issues/188

fjeglinski commented 3 years ago

this is my repl =) reference: #188

Yes! Basically we have the same problem :D

intech commented 3 years ago

I unfortunately did not solve this problem completely, but refused to use stream in my project.

You need to think about the design of the moleculer and work with parameters in streams, there is some separation that is not obvious when working.

fjeglinski commented 3 years ago

That is a pity.

We found a workaround by using the request directly and sending the file data via broker.call from ApiGateway to the particular service which processed file data. It is not a masterpiece ... but it is working.

Maybe I am wrong and maybe I understand the documentation not correctly but it should work the way we both tried isn´t?

intech commented 3 years ago

@fjeglinski yes, there is an unobvious action in which we lose access to the parameters.

icebob commented 3 years ago

0.10.0-beta4: https://github.com/moleculerjs/moleculer-web/blob/master/CHANGELOG.md#multipart-fields--url-params