[Node] 스트림(Stream) 예시
fs.createWriteStream(path[, options]);
const fs = require('fs');
// 파일 생성 혹은 기존 파일 덮어쓰기
const writeStream = fs.createWriteStream('example.txt');
// 파일에 데이터 쓰기
writeStream.write('Hello World\n');
// 파일 쓰기 종료
writeStream.end();
// 파일 쓰기가 완료되면 콘솔에 로그 출력
writeStream.on('finish', () => {
console.log('Data written to file');
});
// 에러 발생 시 콘솔에 로그 출력
writeStream.on('error', (err) => {
console.error(err);
});
const fs = require('fs');
const zlib = require("zlib");
// 파일명 설정
const writeStream = fs.createWriteStream(`example.txt`, { flags: "a" });
// 파일에 데이터 쓰기
writeStream.write('Hello World\n');
writeStream.write('Hello World\n');
// 파일 쓰기 종료
writeStream.end();
// 파일 쓰기가 완료되면 콘솔에 로그 출력
writeStream.on('finish', () => {
console.log('Data written to file');
});
// 에러 발생 시 콘솔에 로그 출력
writeStream.on('error', (err) => {
console.error(err);
});
// Creating readable Stream
const inp = fs.createReadStream('example.txt');
// Creating writable stream
const out = fs.createWriteStream('example.txt.gz');
// Calling createGzip method
const gzip = zlib.createGzip();
// Piping
inp.pipe(gzip).pipe(out);
console.log("Gzip created!");
https://nodejs.org/api/stream.html#stream
Stream | Node.js v19.8.1 Documentation
Source Code: lib/stream.js A stream is an abstract interface for working with streaming data in Node.js. The node:stream module provides an API for implementing the stream interface. There are many stream objects provided by Node.js. For instance, a reques
nodejs.org
[Node] 스트림(Stream)
스트림(Stream)스트림은 배열이나 문자열같은 데이터 컬렉션이다. 스트림은 대용량의 파일을 다룰 때나, 외부 소스로부터 데이터를 한번에 일부분씩 가져올때 사용된다. 스트림이 왜 필요한데? fs
jess2.github.io
Node.js zlib.createGzip() Method - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
https://psyhm.tistory.com/26#recentEntries
[Node.js][노드] stream(스트림) 이란??
Node.js에서 stream이란 "스트리밍 데이터로 작업하기 위한 추상적인 인터페이스"라고 공식 문서에 나와있다. 뭔 소리일까..? 일단 stream이란 개념을 짚고 넘어가야 될 듯 싶다. stream이란 일종의 추상
psyhm.tistory.com