Skip to content

Commit be9405e

Browse files
mahdi-farniaMaledong
andauthored
es6 syntax & replace createClient with request (nodejs#4797)
Co-authored-by: Maledong <[email protected]>
1 parent e6eb595 commit be9405e

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

locale/en/knowledge/advanced/streams/how-to-use-stream-pipe.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,21 @@ Streams make for quite a handy abstraction, and there's a lot you can do with th
1515
```javascript
1616
#!/usr/bin/env node
1717

18-
var child = require('child_process');
18+
const child = require('node:child_process');
1919

20-
var myREPL = child.spawn('node');
20+
const myREPL = child.spawn('node');
2121

2222
myREPL.stdout.pipe(process.stdout, { end: false });
2323

2424
process.stdin.resume();
2525

2626
process.stdin.pipe(myREPL.stdin, { end: false });
2727

28-
myREPL.stdin.on('end', function() {
28+
myREPL.stdin.on('end', () => {
2929
process.stdout.write('REPL stream ended.');
3030
});
3131

32-
myREPL.on('exit', function (code) {
33-
process.exit(code);
34-
});
32+
myREPL.once('exit', code => process.exit(code));
3533
```
3634

3735
There you have it - spawn the Node.js REPL as a child process, and pipe your stdin and stdout to its stdin and stdout. Make sure to listen for the child's 'exit' event, too, or else your program will just hang there when the REPL exits.
@@ -41,10 +39,10 @@ Another use for `stream.pipe()` is file streams. In Node.js, `fs.createReadStrea
4139
```javascript
4240
#!/usr/bin/env node
4341

44-
var child = require('child_process'),
45-
fs = require('fs');
42+
const child = require('node:child_process'),
43+
fs = require('node:fs');
4644

47-
var myREPL = child.spawn('node'),
45+
const myREPL = child.spawn('node'),
4846
myFile = fs.createWriteStream('myOutput.txt');
4947

5048
myREPL.stdout.pipe(process.stdout, { end: false });
@@ -55,13 +53,11 @@ process.stdin.resume();
5553
process.stdin.pipe(myREPL.stdin, { end: false });
5654
process.stdin.pipe(myFile);
5755

58-
myREPL.stdin.on("end", function() {
56+
myREPL.stdin.on("end", () => {
5957
process.stdout.write("REPL stream ended.");
6058
});
6159

62-
myREPL.on('exit', function (code) {
63-
process.exit(code);
64-
});
60+
myREPL.once('exit', code => process.exit(code));
6561
```
6662

6763
With those small additions, your stdin and the stdout from your REPL will both be piped to the writeable file stream you opened to 'myOutput.txt'. It's that simple - you can pipe streams to as many places as you want.
@@ -71,18 +67,17 @@ Another very important use case for `stream.pipe()` is with HTTP request and res
7167
```javascript
7268
#!/usr/bin/env node
7369

74-
var http = require('http');
70+
const http = require('node:http');
7571

76-
http.createServer(function(request, response) {
77-
var proxy = http.createClient(9000, 'localhost')
78-
var proxyRequest = proxy.request(request.method, request.url, request.headers);
79-
proxyRequest.on('response', function (proxyResponse) {
72+
http.createServer((request, response) => {
73+
const proxy = http.request(request.url, { hostname: "localhost", port: 9000, headers: request.headers, method: request.method });
74+
proxy.once('response', proxyResponse => {
8075
proxyResponse.pipe(response);
8176
});
82-
request.pipe(proxyRequest);
77+
request.pipe(proxy);
8378
}).listen(8080);
8479

85-
http.createServer(function (req, res) {
80+
http.createServer((req, res) => {
8681
res.writeHead(200, { 'Content-Type': 'text/plain' });
8782
res.write('request successfully proxied to port 9000!' + '\n' + JSON.stringify(req.headers, true, 2));
8883
res.end();

0 commit comments

Comments
 (0)