Michał Michalczuk
I am fullstack software developer #dotnet #angular #typescript, IT trainer. Both fascinated and terrified by technology advancement.
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
By Michał Michalczuk
No matter what you are doing as front-end developer Node.js is everywhere and you won’t escape from it. On my presentation I will focus on steams - one of the basic, and often not understand, element of this environment. How they work and what benefit you will get by using them as intended.
I am fullstack software developer #dotnet #angular #typescript, IT trainer. Both fascinated and terrified by technology advancement.