{"id":6208,"date":"2025-05-29T14:00:33","date_gmt":"2025-06-04T06:34:32","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6208"},"modified":"2025-06-04T14:00:33","modified_gmt":"2025-06-04T06:34:32","slug":"%e4%ba%86%e8%a7%a3node-jsstream%e6%a8%a1%e5%9d%97%e5%a6%82%e4%bd%95%e8%99%95%e7%90%86%e5%a4%a7%e5%9e%8b%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e4%ba%86%e8%a7%a3node-jsstream%e6%a8%a1%e5%9d%97%e5%a6%82%e4%bd%95%e8%99%95%e7%90%86%e5%a4%a7%e5%9e%8b%e6%95%b8%e6%93%9a\/","title":{"rendered":"\u6df1\u5165\u63a2\u8a0e Node.js Stream \u6a21\u584a\uff1a\u9ad8\u6548\u8655\u7406\u5927\u578b\u6578\u64da\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, stream, \u5927\u578b\u6578\u64da, \u6578\u64da\u8655\u7406, JavaScript\"><\/p>\n<h1>\u6df1\u5165\u63a2\u8a0e Node.js Stream \u6a21\u584a\uff1a\u9ad8\u6548\u8655\u7406\u5927\u578b\u6578\u64da\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Node.js \u662f\u4e00\u500b\u5f37\u5927\u7684 JavaScript \u74b0\u5883\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u985e\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\uff0c\u5305\u62ec Web \u548c\u79fb\u52d5\u61c9\u7528\u3002\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u578b\u6578\u64da\u6642\uff0cNode.js \u7684 Stream \u6a21\u584a\u5c55\u73fe\u51fa\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u6027\u3002\u672c\u6587\u5c07\u5e36\u60a8\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u6709\u6548\u4f7f\u7528 Node.js \u4e2d\u7684 Stream \u6a21\u584a\u4f86\u8655\u7406\u5927\u578b\u6578\u64da\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Stream \u6a21\u584a\uff1f<\/h2>\n<p>Stream \u6a21\u584a\u662f Node.js \u7684\u6838\u5fc3\u7d44\u4ef6\u4e4b\u4e00\uff0c\u5c08\u9580\u8a2d\u8a08\u7528\u4f86\u8655\u7406\u5927\u578b\u6578\u64da\u6d41\u3002\u5b83\u53ef\u4ee5\u5c07\u6578\u64da\u5206\u5272\u6210\u8f03\u5c0f\u7684\u584a\uff0c\u9010\u4e00\u50b3\u8f38\u81f3\u76ee\u7684\u5730\uff0c\u9019\u6a23\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u9084\u80fd\u986f\u8457\u6e1b\u5c11\u7cfb\u7d71\u8cc7\u6e90\u7684\u6d88\u8017\u3002\u9019\u4f7f\u5f97 Stream \u6a21\u584a\u6210\u70ba\u8655\u7406\u5927\u6578\u64da\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Stream \u6a21\u584a\uff1f<\/h2>\n<p>\u4f7f\u7528 Stream \u6a21\u584a\u4f86\u8655\u7406\u5927\u578b\u6578\u64da\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<p>1. \u5275\u5efa\u4e00\u500b Stream \u5c0d\u8c61\uff0c\u4e26\u5c07\u6578\u64da\u50b3\u5165\u8a72\u5c0d\u8c61\u3002<br \/>\n2. \u5c07 Stream \u5c0d\u8c61\u4e2d\u7684\u6578\u64da\u5206\u5272\u6210\u5c0f\u584a\uff0c\u4e26\u9010\u4e00\u50b3\u8f38\u81f3\u76ee\u7684\u5730\u3002<br \/>\n3. \u6839\u64da\u9700\u6c42\u5c0d\u63a5\u6536\u5230\u7684\u6578\u64da\u9032\u884c\u8655\u7406\u3002<br \/>\n4. \u5c07\u8655\u7406\u5f8c\u7684\u6578\u64da\u8fd4\u56de\u7d66 Stream \u5c0d\u8c61\u3002<\/p>\n<h2>\u793a\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 Node.js Stream \u6a21\u584a\u8655\u7406\u5927\u578b\u6578\u64da\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\n\/\/ \u5f15\u5165 Stream \u6a21\u584a<br \/>\nconst { Readable } = require(&#8216;stream&#8217;);<\/p>\n<p>\/\/ \u5275\u5efa\u4e00\u500b Stream \u5c0d\u8c61<br \/>\nconst dataStream = new Readable({<br \/>\n    read() {}<br \/>\n});<\/p>\n<p>\/\/ \u5c07\u6578\u64da\u50b3\u5165 Stream \u5c0d\u8c61<br \/>\ndataStream.push(&#8216;some data&#8217;);<br \/>\ndataStream.push(null); \/\/ \u8868\u793a\u6578\u64da\u7d50\u675f<\/p>\n<p>\/\/ \u8655\u7406\u6578\u64da<br \/>\ndataStream.on(&#8216;data&#8217;, (chunk) => {<br \/>\n    \/\/ \u5728\u6b64\u8655\u7406\u63a5\u6536\u5230\u7684\u6578\u64da<br \/>\n    console.log(`Received chunk: ${chunk}`);<br \/>\n    \/\/ \u8655\u7406\u5f8c\u7684\u6578\u64da\u53ef\u4ee5\u9032\u4e00\u6b65\u4f7f\u7528<br \/>\n});<\/p>\n<p>\/\/ \u8655\u7406\u7d50\u675f\u4e8b\u4ef6<br \/>\ndataStream.on(&#8216;end&#8217;, () => {<br \/>\n    console.log(&#8216;No more data to process.&#8217;);<br \/>\n});<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Stream \u6a21\u584a\u4f86\u9ad8\u6548\u8655\u7406\u5927\u578b\u6578\u64da\u3002\u5b83\u80fd\u5920\u5c07\u6578\u64da\u5206\u5272\u6210\u5c0f\u584a\u4e26\u9010\u4e00\u50b3\u8f38\uff0c\u4e26\u5728\u63a5\u6536\u5230\u6578\u64da\u5f8c\u9032\u884c\u6240\u9700\u7684\u8655\u7406\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86\u5982\u4f55\u4f7f\u7528 Node.js \u4e2d\u7684 Stream \u6a21\u584a\u4f86\u9ad8\u6548\u8655\u7406\u5927\u578b\u6578\u64da\u3002Stream \u6a21\u584a\u7684\u8a2d\u8a08\u4f7f\u5176\u80fd\u5920\u5c07\u5927\u578b\u6578\u64da\u5206\u5272\u70ba\u5c0f\u584a\uff0c\u9010\u4e00\u8655\u7406\uff0c\u4e26\u6709\u6548\u6e1b\u5c11\u7cfb\u7d71\u8cc7\u6e90\u6d88\u8017\u3002\u638c\u63e1\u9019\u4e9b\u6280\u5de7\uff0c\u60a8\u5c07\u80fd\u5920\u5728\u8655\u7406\u5927\u578b\u6578\u64da\u6642\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u8acb\u6ce8\u610f\uff0c\u6240\u6709\u5716\u7247\u7684\u7db2\u5740\u4fdd\u6301\u4e0d\u8b8a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:Node.jsStream\u6a21\u5757\u53ef\u4ee5\u8655\u7406\u5927\u578b\u6578\u64da\uff0c\u5b83\u53ef\u4ee5\u5c07\u5927\u578b\u6578\u64da\u5206\u5272\u6210\u5c0f\u584a\uff0c\u4e26\u6309\u9806\u5e8f\u8655\u7406\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Node.jsStream\u6a21\u5757\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u8655\u7406\u5927\u578b\u6578\u64da\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[193,192],"tags":[191,190],"class_list":["post-6208","post","type-post","status-publish","format-standard","hentry","category-node","category-node-js","tag-node","tag-node-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1C8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6208"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6208\/revisions"}],"predecessor-version":[{"id":6209,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6208\/revisions\/6209"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}