Node.js Stream Module
ဥပမာ
ရေးနိုင်သော လမ်းကြောင်းသို့ ရေးပါ-
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello
World!');
res.end();
}).listen(8080);
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
Stream module သည် streaming data ကို ကိုင်တွယ်သည့်နည်းလမ်းကို ပံ့ပိုးပေးပါသည်။
stream အမျိုးအစား နှစ်မျိုးရှိသည်- readable နှင့် writeable.
readable stream ၏ ဥပမာတစ်ခုသည် http.createServer() method ဖြင့် လုပ်ဆောင်သောအခါ သင်ရရှိသည့် တုံ့ပြန်မှု အရာဝတ္ထုဖြစ်သည်။
စာရေး နိုင်သော stream ၏ ဥပမာတစ်ခုသည် http.createServer() method ဖြင့် လုပ်ဆောင်သောအခါ သင်ရရှိသည့် တောင်းဆိုချက် အရာဝတ္ထုဖြစ်သည်။
အထားအသို
အချို့သောနည်းလမ်းများသည် http.createServer() ကဲ့သို့ readable/writable stream object ကို ပြန်ပို့ပေးပြီး ထိုသို့ဖြစ်ပါက၊ သင်သည် stream module ကို ထည့်သွင်းရန် မလိုအပ်ပါ။
မဟုတ်ပါက၊ သင့်အပလီကေးရှင်းတွင် တိုက်ရိုက်ထုတ်လွှင့်မှု မော်ဂျူး အပါအဝင် အထားအသို-
var stream = require('stream');
Readable Stream Properties နှင့် Methods
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 |
Writable Stream Properties နှင့် Methods
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 |