Michał Michalczuk
@michalczukm
Spartez / Atlassian
infoShare Academy
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
http.IncommingMessage Readible Stream
http.ServerResponse Writable Stream
process.stdin
process.stdout
Readible Stream
Writable Stream
fs.createReadStream(...)
fs.createWriteStream(...)
Readible Stream
Writable Stream
ls | wc
Output
Input
Pipe
ls | wc
process.stdin.pipe(process.stdout);
Readable stream
Writable stream
.pipe( );
process.stdin.pipe(wc);
Writable
Transform
const { Writable } = require('stream');
const outStream = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString().toUpperCase());
callback();
}
});
process.stdin.pipe(outStream);
Readable
Transform
const { Readable } = require('stream');
const inStream = new Readable();
inStream.push('first chunk\n');
inStream.push('second chunk\n');
inStream.push(null);
setTimeout(() => {
inStream.pipe(process.stdout);
}, 2000);
Readable
Writable
Dup lex
Trans form
Duplex
Transform
const { Duplex } = require('stream');
const inoutStream = new Duplex({
write(chunk, encoding, callback) {
console.log(chunk.toString().toUpperCase());
callback();
},
read(size) {
}
});
inoutStream.push('first chunk\n');
inoutStream.push('second chunk\n');
inoutStream.push(null);
process.stdin.pipe(inoutStream).pipe(process.stdout);
Transform
Transform
const { Transform } = require('stream');
const upperCaseTr = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
process.stdin
.pipe(upperCaseTr)
.pipe(process.stdout);
Yes please