Node.js स्ट्रीम मॉड्यूल
उदाहरण
लिखने योग्य स्ट्रीम में लिखें:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello
World!');
res.end();
}).listen(8080);
परिभाषा और उपयोग
स्ट्रीम मॉड्यूल स्ट्रीमिंग डेटा को संभालने का एक तरीका प्रदान करता है।
धाराएँ दो प्रकार की होती हैं: पठनीय और लिखने योग्य।
पठनीय स्ट्रीम का एक उदाहरण http.createServer() पद्धति के साथ काम करने पर आपको मिलने वाली प्रतिक्रिया वस्तु है।
लिखने योग्य स्ट्रीम का एक उदाहरण अनुरोध ऑब्जेक्ट है जो आपको http.createServer() पद्धति के साथ कार्य करते समय प्राप्त होता है।
वाक्य - विन्यास
कुछ विधियाँ एक पठनीय/लिखने योग्य स्ट्रीम ऑब्जेक्ट लौटाती हैं, जैसे http.createServer(), और यदि ऐसा है, तो आपको स्ट्रीम मॉड्यूल को शामिल करने की आवश्यकता नहीं है।
अन्यथा, आपके एप्लिकेशन में स्ट्रीम मॉड्यूल को शामिल करने के लिए सिंटैक्स:
var stream = require('stream');
पठनीय स्ट्रीम गुण और तरीके
Method | Description |
---|---|
isPaused() | Returns true if the state of the readable stream is paused, otherwise false |
pause() | Pauses the readable stream |
pipe() | Turns the readable stream into the specified writable stream |
read() | Returns a specified part of the readable stream |
resume() | Resumes a paused stream |
setEncoding() | Sets the character encoding of the readable stream |
unpipe() | Stops turning a readable stream into a writable stream, caused by the pipe() method |
unshift() | Pushes some specified data back into the internal buffer |
wrap() | Helps reading streams made by older Node.js versions |
लिखने योग्य स्ट्रीम गुण और तरीके
Method | Description |
---|---|
cork() | Stops the writable stream and all written data will be buffered in memory |
end() | Ends the writable stream |
setDefaultEncoding() | Sets the encoding for the writable stream |
uncork() | Flushes all data that has been buffered since the cork() method was called |
write() | Writes data to the stream |