-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.js
41 lines (33 loc) · 1.28 KB
/
pipeline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { pipeline } = require('stream');
const express = require('express');
const CounterReadableStream = require('./CounterReadableStream');
const CollectSizeStream = require('./CollectSizeStream');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
const counterReadableStream = new CounterReadableStream();
const collectSizeStream = new CollectSizeStream();
counterReadableStream.on('end', () => console.log('end counterStream'));
counterReadableStream.on('close', () => console.log('close counterStream'));
counterReadableStream.on('error', (err) => console.error('counterStream', err));
collectSizeStream.on('end', () => console.log('end collectSizeStream'));
collectSizeStream.on('close', () => console.log('close collectSizeStream'));
collectSizeStream.on('error', (err) => console.error('collectSizeStream', err));
res.on('close', () => console.log('close writableStream'));
res.on('error', (err) => console.error('writableStream', err));
pipeline(
counterReadableStream,
collectSizeStream,
res,
(err) => {
if (err) {
console.error('Pipeline failed', err);
} else {
console.log('Pipeline succeeded');
}
},
);
});
app.listen(port, () => {
console.log(`Sample app listening on port ${port}.`);
});