Node.js 스트림 모듈
예시
쓰기 가능한 스트림에 쓰기:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello
World!');
res.end();
}).listen(8080);
정의 및 사용
Stream 모듈은 스트리밍 데이터를 처리하는 방법을 제공합니다.
스트림에는 읽기 및 쓰기의 두 가지 유형이 있습니다.
읽을 수 있는 스트림의 예는 http.createServer() 메서드로 작업할 때 얻는 응답 객체입니다.
쓰기 가능한 스트림의 예는 http.createServer() 메소드로 작업할 때 얻는 요청 객체입니다.
통사론
일부 메소드는 http.createServer()와 같은 읽기/쓰기 가능한 스트림 객체를 반환하며, 이 경우 스트림 모듈을 포함할 필요가 없습니다.
그렇지 않은 경우 애플리케이션에 Stream 모듈을 포함하는 구문은 다음과 같습니다.
var stream = require('stream');
읽을 수 있는 스트림 속성 및 메서드
Method | Description |
---|---|
isPaused() | Returns true if the state of the readable stream is paused, otherwise false |
pause() | Pauses the readable stream |
pipe() | Turns the readable stream into the specified writable stream |
read() | Returns a specified part of the readable stream |
resume() | Resumes a paused stream |
setEncoding() | Sets the character encoding of the readable stream |
unpipe() | Stops turning a readable stream into a writable stream, caused by the pipe() method |
unshift() | Pushes some specified data back into the internal buffer |
wrap() | Helps reading streams made by older Node.js versions |
쓰기 가능한 스트림 속성 및 메서드
Method | Description |
---|---|
cork() | Stops the writable stream and all written data will be buffered in memory |
end() | Ends the writable stream |
setDefaultEncoding() | Sets the encoding for the writable stream |
uncork() | Flushes all data that has been buffered since the cork() method was called |
write() | Writes data to the stream |