Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pipe stream #112

Open
robertsLando opened this issue Jul 7, 2020 · 4 comments
Open

How to pipe stream #112

robertsLando opened this issue Jul 7, 2020 · 4 comments

Comments

@robertsLando
Copy link

I'm looking for a way to proxy requests using bent. With request it was working like:

var request = require('request');

app.use('/api', function(req, res) {
  var url = apiUrl + req.url;
  req.pipe(request(url)).pipe(res);
});

@mikeal
Copy link
Owner

mikeal commented Jul 7, 2020

bent returns a stream by default, but unlike request it doesn’t instrument the pipe() command to set the method and headers on a subsequent http stream, so you’ll need to do that yourself.

@robertsLando
Copy link
Author

robertsLando commented Jul 7, 2020 via email

@robertoentringer
Copy link

Hi @robertsLando take this example :

const fs = require('fs')
const { promisify } = require('util')
const stream = require('stream')
const pipeline = promisify(stream.pipeline)
const bent = require('bent')

const main = async () => {
  const readable = await bent('https://images.unsplash.com/photo-1595132938692-83ad2c098e47')()
  const writable = fs.createWriteStream('./image.jpg')
  await pipeline(readable, writable)
}

main()

That works for me !

@czzonet
Copy link

czzonet commented Feb 19, 2021

Hi @robertsLando take this example :

const fs = require('fs')
const { promisify } = require('util')
const stream = require('stream')
const pipeline = promisify(stream.pipeline)
const bent = require('bent')

const main = async () => {
  const readable = await bent('https://images.unsplash.com/photo-1595132938692-83ad2c098e47')()
  const writable = fs.createWriteStream('./image.jpg')
  await pipeline(readable, writable)
}

main()

That works for me !

Thanks, it works well in ts:

import bent from "bent";
import { createWriteStream } from "fs";
import { resolve } from "path";
import { pipeline } from "stream";
import { promisify } from "util";

const pipe = promisify(pipeline);

const URL = "http://***/1.txt";

export const run = async () => {
  const readable = (await bent(URL)("")) as NodeJS.ReadableStream;
  const writeable = await createWriteStream(resolve(__dirname, "../2.txt"));

  await pipe(readable, writeable);

  console.log("0");
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants