{"id":6158,"date":"2023-01-07T11:12:05","date_gmt":"2023-01-07T03:12:05","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6158"},"modified":"2023-01-07T11:12:05","modified_gmt":"2023-01-07T03:12:05","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8streamapi","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8streamapi\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528StreamAPI"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, Stream API\"><\/p>\n<h1>\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 Stream API\uff1f<\/h1>\n<p>Node.js \u662f\u4e00\u500b\u5ee3\u53d7\u6b61\u8fce\u7684 JavaScript \u57f7\u884c\u74b0\u5883\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u7db2\u8def\u61c9\u7528\u7a0b\u5f0f\u3002Node.js \u63d0\u4f9b\u4e86\u4e00\u500b Stream API\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u8655\u7406\u8cc7\u6599\u6d41\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 Stream API\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Stream API\uff1f<\/h2>\n<p>Stream API \u662f Node.js \u4e2d\u7684\u4e00\u500b\u6a21\u7d44\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u8655\u7406\u8cc7\u6599\u6d41\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u8cc7\u6599\u6d41\u5206\u5272\u6210\u5c0f\u584a\uff0c\u4e26\u5c0d\u6bcf\u500b\u5c0f\u584a\u505a\u51fa\u4e0d\u540c\u7684\u8655\u7406\u3002Stream API \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u8cc7\u6599\u6d41\uff0c\u4e26\u6e1b\u5c11\u8a18\u61b6\u9ad4\u7684\u4f7f\u7528\u91cf\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Stream API\uff1f<\/h2>\n<p>\u4f7f\u7528 Stream API \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5169\u500b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li><strong>\u5275\u5efa Stream \u5c0d\u8c61\uff1a<\/strong>\u9996\u5148\uff0c\u958b\u767c\u8005\u9700\u8981\u5275\u5efa\u4e00\u500b Stream \u5c0d\u8c61\uff0c\u4e26\u5c07\u8cc7\u6599\u6d41\u50b3\u5165 Stream \u5c0d\u8c61\u4e2d\u3002<\/li>\n<li><strong>\u8655\u7406\u8cc7\u6599\u6d41\uff1a<\/strong>\u63a5\u8457\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 Stream API \u4e2d\u7684\u65b9\u6cd5\u5c0d\u8cc7\u6599\u6d41\u9032\u884c\u8655\u7406\uff0c\u4f8b\u5982\u5c07\u8cc7\u6599\u6d41\u5206\u5272\u6210\u5c0f\u584a\uff0c\u4e26\u5c0d\u6bcf\u500b\u5c0f\u584a\u505a\u51fa\u4e0d\u540c\u7684\u8655\u7406\u3002<\/li>\n<\/ul>\n<h2>\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u4e86\u89e3\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 Stream API\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst fs = require('fs');\nconst stream = fs.createReadStream('data.txt');\n\nstream.on('data', (chunk) => {\n    console.log(chunk);\n});\n\nstream.on('end', () => {\n    console.log('end');\n});\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>fs.createReadStream()<\/code> \u65b9\u6cd5\u5275\u5efa\u4e86\u4e00\u500b Stream \u5c0d\u8c61\uff0c\u4e26\u5c07\u8cc7\u6599\u6d41\u50b3\u5165 Stream \u5c0d\u8c61\u4e2d\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528 <code>stream.on()<\/code> \u65b9\u6cd5\u5c0d\u8cc7\u6599\u6d41\u9032\u884c\u8655\u7406\uff0c\u5c07\u8cc7\u6599\u6d41\u5206\u5272\u6210\u5c0f\u584a\uff0c\u4e26\u5c0d\u6bcf\u500b\u5c0f\u584a\u505a\u51fa\u4e0d\u540c\u7684\u8655\u7406\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 Stream API\u3002Stream API \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u8cc7\u6599\u6d41\uff0c\u4e26\u6e1b\u5c11\u8a18\u61b6\u9ad4\u7684\u4f7f\u7528\u91cf\u3002\u4f7f\u7528 Stream API \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5169\u500b\u6b65\u9a5f\uff1a\u5275\u5efa Stream \u5c0d\u8c61\uff0c\u4e26\u5c0d\u8cc7\u6599\u6d41\u9032\u884c\u8655\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528StreamAPI\uff0c\u5f9e\u57fa\u672c\u6982\u5ff5\u5230\u5be6\u969b\u61c9\u7528\uff0c\u8b93\u4f60\u66f4\u52a0\u4e86\u89e3Node.js\u4e2d\u7684StreamAPI\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-6158","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-1Bk","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6158","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=6158"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6158\/revisions"}],"predecessor-version":[{"id":6159,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6158\/revisions\/6159"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}