लिया तुम सिर्फ फ़ाइल का आकार लाने चाहते हैं एक के बजाय एक प्रमुख अनुरोध करने के लिए की जरूरत है, यह सबसे अच्छा है 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 हेड का उपयोग करना अधिक कुशल है।
स्रोत
2013-04-24 14:33:40
धन्यवाद। प्रतिक्रिया.एंड() से यह अलग कैसे है और मुझे इसका उपयोग कब करना चाहिए? – Tushar
एक और बात, अगर मैं 'डेटा' ईवेंट में श्रोता को बाध्य नहीं करता तो डेटा अभी भी स्थानांतरित हो जाएगा? मेरा मतलब है कि मेरी बैंडविड्थ अनावश्यक रूप से बर्बाद हो जाएगी? – Tushar
हां, डेटा अभी भी ग्राहक को भेजा जाएगा भले ही आप "डेटा" ईवेंट को संभाल नहीं लेते हैं। –