Closed ccarruitero closed 4 years ago
I think we can do that. I write the code below, and it passes the existing test.
const http = require('http');
const options = {
hostname: 'localhost',
port: 8099,
method: 'POST'
}
const req = http.request(options, (res) => {
res.on('data', (chunk) => {
process.stdout.write(chunk);
});
});
process.stdin.pipe(req);
I am thinking that is it possible for http module to pipe directly from stdin to stream then to stdout as below (something like process.stdin.pipe(req).pipe(process.stdout)
)?
Because I think it would better describe the concept of stream. If I understand the http module correctly, it seems not possible to do the same trick here. What do you think?
Hi @jordanSu
Thanks for take a look into this. Do you mind to start a PR for this?
You can't pipe http.request()
directly to stdout
since http.request()
give you a http.ClientRequest
, that is a writable stream; and you need to pipe from a readable stream or a duplex.
Instead you could pipe the response
of the request to stdout
.
const req = request('http://localhost:8099', { method: 'POST' }, (res) => res.pipe(process.stdout))
process.stdin.pipe(req)
Hi @ccarruitero I start a PR #236 for this issue.
Please help review it, and any advice is welcome, especially the problem description part.
request is deprecated
These dependency is used only in
http_client
exercise. I think we should usehttp.request
instead