{"id":106535,"date":"2020-05-28T18:27:23","date_gmt":"2020-05-28T10:27:23","guid":{"rendered":"http:\/\/4563.org\/?p=106535"},"modified":"2020-05-28T18:27:23","modified_gmt":"2020-05-28T10:27:23","slug":"node-js-%e4%b8%80%e8%a1%8c%e4%b8%80%e8%a1%8c%e5%9c%b0%e8%af%bb%e5%8f%96%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=106535","title":{"rendered":"Node.js \u4e00\u884c\u4e00\u884c\u5730\u8bfb\u53d6\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Node.js \u4e00\u884c\u4e00\u884c\u5730\u8bfb\u53d6\u6587\u672c\u6587\u4ef6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : youmoo <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728 Node.js \u4e2d\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u7684\u662f:<\/p>\n<ol>\n<li><code>fs.readFile<\/code><\/li>\n<li><code>fs.createReadStream<\/code><\/li>\n<\/ol>\n<p>\u4f46\u8fd9 2 \u4e2a\u65b9\u6cd5\u90fd\u6ca1\u6709\u5bf9\u6309\u884c\u8bfb\u53d6\u7684\u652f\u6301\uff0c\u600e\u4e48\u529e\uff1f<\/p>\n<p>Node.js \u5185\u7f6e\u4e86\u4e00\u4e2a <strong>readline<\/strong> \u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u3002<\/p>\n<p>\u66f4\u8be6\u7ec6\u7684\u60c5\u666f\u8bf4\u660e\uff0c\u6211\u5199\u4e86 \u4e00\u7bc7\u6587\u7ae0\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1772271\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gimp <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5443&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1772272\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : MidLinn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u989d\uff0c\u4e0d\u662f\u53ef\u4ee5 fs.open \u7136\u540e fs.read ?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1772273\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sunzongzheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             http:\/\/nodejs.cn\/api\/readline.html                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1772274\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LennieChoi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u524d\u9635\u5b50\u521a\u7528\u5230 readline\uff0c\u6211\u5206\u4eab\u4e0b\u6211\u78b0\u5230\u7684\u95ee\u9898\u3002<br \/>\u516c\u53f8\u7684\u73af\u5883\u662f node v10.15 \u7684\uff0c\u53d1\u73b0\u8fd9\u7248\u672c readline.createInterface \u521b\u5efa\u51fa\u6765\u7684\u4e0d\u662f async iterator \u5bf9\u8c61\uff0c\u65e0\u6cd5\u5bf9\u4ed6 for await \u904d\u5386(v10.16 \u53ef\u4ee5\uff0c\u5c31\u5dee 0.1 \u7248\u672c\uff0c\u54ed)\u3002\u9886\u5bfc\u8fd8\u4e0d\u8ba9\u5347\u7ea7\uff0c\u56e0\u4e3a\u662f\u751f\u4ea7\u73af\u5883\u4e0b\u8981\u8dd1\u7684\u811a\u672c\uff0c\u4e0d\u80fd\u968f\u4fbf\u5347 node \u7248\u672c\u3002<br \/>\u8fd9\u91cc\u6211\u7684\u505a\u6cd5\u662f\u7528 stream.PassThrough \u5957\u4e00\u5c42\uff0creadline \u8bfb\u51fa\u6765\u7684\u6570\u636e\u5199\u8fdb stream \u91cc\uff0c\u7136\u540e for await \u8fd9\u4e2a stream \u5bf9\u8c61\u3002<br \/>function readliner () {<br \/> const output = new stream.PassThrough({ objectMode: true })<br \/> const rl = readline.createInterface({<br \/> input: fs.createReadStream(&#8216;filepath&#8217;)<br \/> })<\/p>\n<p> rl.on(&#8216;line&#8217;, async (line) =&gt; {<br \/> output.write(line)<br \/> })<br \/> rl.on(&#8216;close&#8217;, () =&gt; {<br \/> output.push(&#8216;end&#8217;)<br \/> });<br \/> return output<br \/> }<\/p>\n<p>\u6ce8\u610f\uff1arl.on(&#8216;close&#8217;) \u4e8b\u4ef6\u91cc\u5199\u4e2a end \u6807\u8bb0\u8fdb\u53bb\uff0c\u4ee5\u4fbf\u518d\u5916\u5c42\u904d\u5386\u7684\u65f6\u5019\u78b0\u5230 end \u6807\u8bb0\u4e86\u53ef\u4ee5 break \u6389                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Node.js \u4e00\u884c\u4e00\u884c\u5730\u8bfb\u53d6\u6587\u672c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/106535"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=106535"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/106535\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=106535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=106535"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=106535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}