hapijs / h2o2

Proxy handler for hapi.js
Other
165 stars 66 forks source link

Question : pipe vs h2o2 #99

Closed x4080 closed 5 years ago

x4080 commented 5 years ago

Hi, I wondered if proxy in h2o2 is the same as in express using pipe ? Since I tried to migrate my express server with proxy using pipe, now using h2o2, and I found that if there's 2 requests or more via h2o2 proxy at the same time, it will only pass the 1st one ?

I'm not sure what really happened but in express with pipe, it works fine but cannot work with h2o2 (missing request)

Thanks

hueniverse commented 5 years ago

IIRC, express pipe is just a stream pipe and has nothing to do with proxying. If you pipe the stream you get from a request call, it is a semi-proxy.

Need code to help you more.

x4080 commented 5 years ago

Thanks for helping me out, this is my code in express, how can we do the same thing in hapi ?

  server.post('/api2/*', (req, res) => {
    try {
      req.pipe(request({ url: 'http://localhost:999/' + req.params[0] }).on('error', error => {
        console.log(error.message);
      })).pipe(res);
    } catch (err) {
      console.log(err)
      next(err)
    }

  })
hueniverse commented 5 years ago

In hour route handler just use path /api2/{path*}

const handler = function (request, h) {
    return request({ url: 'http://localhost:999/' + request.params.path });
};

Basically, in hapi you can just return a readable stream and it will do the rest for you.

With h2o2 you can make this even simpler and more configurable with control over header, etc.

More about path parameters: https://hapi.dev/api/?v=18.3.1#path-parameters

x4080 commented 5 years ago

Hi, I tried this solution before, and when I tried it again it shows this :

Debug: internal, implementation, error
    Error: Stream must have a readable interface
    at module.exports.internals.Response._marshal (D:\reactprojects\nextjs9\node_modules\@hapi\hapi\lib\response.js:534:28)
    at exports.content (D:\reactprojects\nextjs9\node_modules\@hapi\hapi\lib\headers.js:43:24)
    at Object.internals.marshal (D:\reactprojects\nextjs9\node_modules\@hapi\hapi\lib\transmit.js:41:15)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
hueniverse commented 5 years ago

I haven't used request in years so not sure how to get the response stream from it vs the write stream. I don't have the time to go and figure out how to use it. I would suggest you use the wreck module instead. Request is not really a valid choice in async/await setup.

x4080 commented 5 years ago

Thanks, I'll try that

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.