2013-04-24 19 views
5

हम सर्वर से शेष प्रतिक्रिया को कैसे रोक सकते हैं - उदाहरण के लिए।नोडजेस अनुरोध में डेटा डाउनलोड करना बंद करें

http.get(requestOptions, function(response){ 

//Log the file size; 
console.log('File Size:', response.headers['content-length']); 

// Some code to download the remaining part of the response? 

}).on('error', onError); 

मैं सिर्फ फ़ाइल आकार लॉग करना चाहता हूं और शेष फ़ाइल डाउनलोड करने में अपनी बैंडविड्थ बर्बाद नहीं करना चाहता हूं। क्या नोडज स्वचालित रूप से इसे संभालता है या क्या मुझे इसके लिए कुछ विशेष कोड लिखना है?

उत्तर

9

लिया तुम सिर्फ फ़ाइल का आकार लाने चाहते हैं एक के बजाय एक प्रमुख अनुरोध करने के लिए की जरूरत है, यह सबसे अच्छा है HTTP HEAD, उपयोग करने के लिए जो शरीर के बिना सर्वर से केवल प्रतिक्रिया शीर्षलेख देता है।

आप इस तरह Node.js में एक HEAD अनुरोध कर सकते हैं:

var http = require("http"), 
    // make the request over HTTP HEAD 
    // which will only return the headers 
    requestOpts = { 
    host: "www.google.com", 
    port: 80, 
    path: "/images/srpr/logo4w.png", 
    method: "HEAD" 
}; 

var request = http.request(requestOpts, function (response) { 
    console.log("Response headers:", response.headers); 
    console.log("File size:", response.headers["content-length"]); 
}); 

request.on("error", function (err) { 
    console.log(err); 
}); 

// send the request 
request.end(); 

संपादित करें:

मैंने महसूस किया कि मैं नहीं वास्तव में अपने प्रश्न है, जो अनिवार्य है "कैसे कर जवाब देने के लिए किया था मैं जल्दी से Node.js में एक अनुरोध समाप्त कर दिया? "। आप response.destroy बुला() द्वारा प्रसंस्करण के बीच में किसी भी अनुरोध को समाप्त कर सकते हैं:

var request = http.get("http://www.google.com/images/srpr/logo4w.png", function (response) { 
    console.log("Response headers:", response.headers); 

    // terminate request early by calling destroy() 
    // this should only fire the data event only once before terminating 
    response.destroy(); 

    response.on("data", function (chunk) { 
     console.log("received data chunk:", chunk); 
    }); 
}); 

आप को नष्ट() कॉल बाहर टिप्पणी और अवलोकन है कि एक पूर्ण अनुरोध में दो हिस्सा लौटा दिए जाते हैं करके इसकी जांच कर सकते हैं। कहीं और उल्लेख किया गया है, हालांकि, HTTP हेड का उपयोग करना अधिक कुशल है।

+0

धन्यवाद। प्रतिक्रिया.एंड() से यह अलग कैसे है और मुझे इसका उपयोग कब करना चाहिए? – Tushar

+0

एक और बात, अगर मैं 'डेटा' ईवेंट में श्रोता को बाध्य नहीं करता तो डेटा अभी भी स्थानांतरित हो जाएगा? मेरा मतलब है कि मेरी बैंडविड्थ अनावश्यक रूप से बर्बाद हो जाएगी? – Tushar

+0

हां, डेटा अभी भी ग्राहक को भेजा जाएगा भले ही आप "डेटा" ईवेंट को संभाल नहीं लेते हैं। –

3

आप मिल

से this answer

var http = require('http'); 
var options = { 
    method: 'HEAD', 
    host: 'stackoverflow.com', 
    port: 80, 
    path: '/' 
}; 
var req = http.request(options, function(res) { 
    console.log(JSON.stringify(res.headers)); 
    var fileSize = res.headers['content-length'] 
    console.log(fileSize) 
    } 
); 
req.end(); 
+0

धन्यवाद नोहा, मुझे इस दृष्टिकोण से अवगत नहीं था। उत्तर के लिए – Tushar

संबंधित मुद्दे