2013-01-01 14 views
0

node.js 스크립트를 사용하여 .js 파일을 다운로드하려고하지만 파일의 일부만 다운로드 중입니다. 여기 Node.js 파일 다운로드가 "require"문으로 잘립니다.

response.on('data', function (chunk) { 
    out.write(chunk); 
    var theModule = require(__dirname + "/" + filename); 
    //less than half of the file is downloaded when the above line is included. 
}); 

의 전체 소스 코드입니다 :

var http = require('http'); 
var fs = require('fs'); 

downloadModule("functionChecker.js"); 

function downloadModule(filename) { 
    var google = http.createClient(80, 'www.google.com'); 
    var request = google.request('GET', '/svn/' + filename, { 
     'host': 'javascript-modules.googlecode.com' 
    }); 
    request.end(); 
    out = fs.createWriteStream(filename); 
    request.on('response', function (response) { 
     response.setEncoding('utf8'); 
     response.on('data', function (chunk) { 
      out.write(chunk); 
      var theModule = require(__dirname + "/" + filename); 
      //less than half of the file is downloaded when the above line is included. 
      //If the import statement is omitted, then the file is downloaded normally. 
     }); 
    }); 
} 
+0

"require"기능으로 파일 다운로드가 중단되고 파일이 완전히 다운로드되기 전에 잘 렸습니다. 나는 아직도이 문제를 해결하는 방법을 알아 내지 못했다. –

답변

1

data 이벤트가 호출 할 수있는 여러 번

특히이 문제를 일으키는 것으로 보인다 부분이다. 모든 데이터가 기록 될 때까지 기다려야합니다. the docs 언급 로서도

response.setEncoding('utf8'); 
response.on('data', function (chunk) { 
    out.write(chunk); 
}); 
response.on('end', function(){ 
    out.end(); 
    var theModule = require(__dirname + "/" + filename); 
}); 

createClient은 중단된다. 논리를 단순화하려면 pipe을 사용하는 것이 좋습니다.

+0

여기서'pipe' 함수는 무엇을합니까? –

+0

@AndersonGreen 문서는 다음 위치에 있습니다. http://nodejs.org/api/stream.html#stream_stream_pipe_destination_options – loganfsmyth

+0

비동기로 파일을 동 기적으로 다운로드하는 방법이 있습니까 (여기 참조)? –

관련 문제