나는 Node.js를에 대한 일 처리하고있어 파일을 읽고 나는 그것이 존재 함을 설립했고 writeHead에 적절한 MIME 타입을 전송 한 후, 와이어를 내려 보내는 두 가지 방법을 발견했다 :node.js의 fs.createReadStream 대 fs.readFile의 장단점은 무엇입니까?
// read the entire file into memory and then spit it out
fs.readFile(filename, function(err, data){
if (err) throw err;
response.write(data, 'utf8');
response.end();
});
// read and pass the file as a stream of chunks
fs.createReadStream(filename, {
'flags': 'r',
'encoding': 'binary',
'mode': 0666,
'bufferSize': 4 * 1024
}).addListener("data", function(chunk) {
response.write(chunk, 'binary');
}).addListener("close",function() {
response.end();
});
을
문제가되는 파일이 비디오처럼 크다면 fs.createReadStream이 더 나은 사용자 환경을 제공한다고 가정하면 정확합니까? 블록화가 덜한 것처럼 느껴집니다. 이게 사실인가요? 다른 프로, 단점, 경고 또는 내가 알아야 할 잡화가 있습니까?
굉장합니다, 고마워요! –
+1 매우 유익합니다. 감사. – styfle
@isaacs, 세 단계를 구현하는 방법에 대한 예를 제공해 주시겠습니까? 감사합니다. –