2011-05-21 33 views
10

我使用強大的接收文件上傳與node.js.我將一些字段與多部分請求中的文件一起發送。用Node.js /強大的中止用戶請求

只要某些字段到達,我就可以驗證請求的真實性,如果這樣做不正確以避免資源浪費,我想中止整個請求。

我還沒有找到正確的方法來中止傳入的請求。我試圖用req.connection.destroy();如下:

form 
.on('field', function(field, value) { 
    fields[field] = value; 
    if (!fields['token'] || !fields['id'] || !fields['timestamp']) { 
     return; 
    } 
    if (!validateToken(fields['token'], fields['id'], fields['timestamp'])) { 
     res.writeHead(401, {'Content-Type' : 'text/plain' }); 
     res.end('Unauthorized'); 
     req.connection.destroy(); 
    } 
}) 

然而,這觸發以下錯誤:

events.js:45 
     throw arguments[1]; // Unhandled 'error' event 
        ^
Error: Cannot resume() closed Socket. 
    at Socket.resume (net.js:764:11) 
    at IncomingMessage.resume (http.js:254:15) 
    at IncomingForm.resume (node_modules/formidable/lib/incoming_form.js:52:11) 
    at node_modules/formidable/lib/incoming_form.js:181:12 
    at node_modules/formidable/lib/file.js:51:5 
    at fs.js:1048:7 
    at wrapper (fs.js:295:17) 

我也試過req.connection.end()但該文件停止上傳。

有什麼想法?提前致謝!

+0

我也對此感興趣。你找到了解決方案嗎? – Nemok 2011-09-24 14:44:07

+0

不幸的是,沒有。 – Kamchatka 2011-09-26 20:01:16

+0

我幾乎準備爲這個問題寫一個重複的問題?任何想法到目前爲止? – 2013-04-28 19:27:20

回答

4

問題是,強大的不理解,你希望它停止。試試這個:

req.connection.destroy(); 
req.connection.resume = function(){}; 

當然,這是一個有點醜陋的解決方法,我會open an issue on github