2014-12-08 101 views
0

我有一臺服務器正在處理大文件,它支持範圍請求。如何修復錯誤:「錯誤:啓動必須<=結束」

但是,當它即將完成,是的NodeJS引發此錯誤:

fs.js:1488 
     throw new Error('start must be <= end'); 
      ^
Error: start must be <= end 
    at new ReadStream (fs.js:1488:13) 
    at Object.fs.createReadStream (fs.js:1449:10) 
    at inicioStream (/home/harold/fileserver/app.js:639:17) 
    at fn (/home/harold/node_modules/async/lib/async.js:641:34) 
    at Object._onImmediate (/home/harold/node_modules/async/lib/async.js:557:34) 
    at processImmediate [as _immediateCallback] (timers.js:345:15) 

我想知道它是什麼以及如何解決它。

這是生成該錯誤代碼的一部分:

var path = __dirname + '/backup/'+location; 
var rs = fs.createReadStream(path, {start: download.range.start, end: download.range.end}); 

謝謝!

+0

你能具體談談你的意思是「支持範圍請求」是什麼? 它看起來像一個無效的範圍(開始大於結束的範圍)已通過,但沒有更多信息,我不能說它來自哪裏。 – wisew 2014-12-08 02:16:36

+0

用戶可以發出部分請求。使用「Range」http頭。 – 2014-12-08 02:18:43

回答

0

由於用戶可以通過Range HTTP標頭對這些大文件進行部分請求,因此只要該範圍恰好無效,就會拋出此錯誤。在這種情況下,所請求的範圍無效,因爲download.range.start的值大於download.range.end,因此它必須始終處於相反的位置。

因爲總是有可能出現這個錯誤的可能性,你需要抓住它在你的代碼:

var rs = fs.createReadStream(path, {start: download.range.start, end: download.range.end}); 
rs.on('error', function(err) { 
    // code to handle error - maybe return a response saying the range is invalid? 
}); 
+0

我沒有想過這種可能性,並會修復我的代碼,但現在情況並非如此。下載尚未完成,並在文件被下載99,99%時拋出此錯誤。下載完畢。我不想破壞我的代碼,還沒有。 – 2014-12-08 02:35:49

+0

嗯,我不知道那是怎麼回事。你介意用'download'的值更新你的代碼嗎? – wisew 2014-12-08 02:43:00

+0

其實看着[fs.createReadStream](http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options)的文檔,我想知道你是否需要在'options'對象中傳遞'encoding'屬性太。 – wisew 2014-12-08 02:44:50