Thursday 20 April 2017

Hapijs Forwarding a stream to another server

I try to upload a image to my Hapijs Server via stream and send(pipe) the data to another Hapijs instance.

So I have this post Route, which should receive a stream and forward it:

 this.server.route({
            method: "POST",
            path: RouteManager.Routes.SearchRoute.User.Icon.post,
            config: {
                payload: {
                    output: 'file',
                    parse: true,
                    allow: 'multipart/form-data',
                    maxBytes: 4194304 //4MB
                },
                handler: (request, reply) => {
                    const data = request.payload.image;
                    const req = require("request").post("myOtherServer.com" + "/upload",
                    {
                      formData: {
                        image: data
                    }
                }, (err, httpResp, body) => {
                    cb(err, JSON.parse(body));
                });
                }
            }
        })

My route on the other Server looks like this:

 this.hapi.route({
            method: 'POST',
            path: Routes.index.child.upload.post,
            config: {
                payload: {
                    output: 'stream',
                    parse: true,
                    allow: 'multipart/form-data',
                    maxBytes: 4194304 //4MB
                }
                }
            },
            handler: (request, reply) => {       
                    this.workWithStream(request, reply);
            }
        });

When i run the code i get always this response:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Invalid multipart payload format"
}

How is it possible to forwarding a stream to another hapijs server(without to save a temporary file)?

Thanks



via laren0815

No comments:

Post a Comment