{"id":188,"date":"2021-02-10T10:06:58","date_gmt":"2021-02-10T02:06:58","guid":{"rendered":"http:\/\/gjweb.top\/?p=188"},"modified":"2021-02-10T10:06:59","modified_gmt":"2021-02-10T02:06:59","slug":"node%e4%b8%ad%e7%9a%84%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e5%86%99%e5%85%a5","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=188","title":{"rendered":"node\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c(\u5199\u5165)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">node\u4e2d\u7684\u6587\u4ef6\u7cfb\u7edf<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728Node\u4e2d\u6709\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf,\u6240\u8c13\u7684\u6587\u4ef6\u7cfb\u7edf,\u5c31\u662f\u5bf9\u8ba1\u7b97\u673a\u4e2d\u6587\u4ef6\u8fdb\u884c\u589e\u5220\u6539\u67e5<\/li><li>\u5728node\u4e2d,\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6a21\u5757,fs(\u6587\u4ef6\u7cfb\u7edf)<\/li><li>fs \u662fnode\u6838\u5fc3\u6a21\u5757,\u65e0\u9700\u4e0b\u8f7d,<strong>\u9700\u8981\u5f15\u5165<\/strong><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b80\u5355\u6587\u4ef6\u5199\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*<br>    \/\/\u5f02\u6b65\u6587\u4ef6\u5199\u5165<br>    fs.writeFile(file,data[,options],callback){<br>        file  \u8981\u5199\u5165\u7684\u6587\u4ef6\u8def\u5f84 + \u6587\u4ef6\u540d<br>        data \u8981\u5199\u5165\u7684\u6570\u636e<br>        options \u53ef\u9009\u53c2\u6570[\u914d\u7f6e\u5bf9\u8c61]<br>            encoding:\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f,\u9ed8\u8ba4\u503c utf8<br>            mode: \u8bbe\u7f6e\u6587\u4ef6\u7684\u64cd\u4f5c\u6743\u9650,\u9ed8\u8ba4\u503c 0o666(\u53ef\u8bfb\u53ef\u53d6)<br>                -- 0o111 \u88ab\u6267\u884c\u7684\u6743\u9650<br>                -- 0o222 \u88ab\u5199\u5165\u7684\u6743\u9650<br>                -- 0o444 \u88ab\u8bfb\u53d6\u7684\u6743\u9650<br>            flag : \u6253\u5f00\u6587\u4ef6\u8981\u6267\u884c\u7684\u64cd\u4f5c,\u9ed8\u8ba4\u503c\u662f'w'<br>                -- a \u8ffd\u52a0<br>                -- w \u5199\u5165<br>        callback \u56de\u8c03\u51fd\u6570<br>            err \u591a\u65e0\u5bf9\u8c61<br>    }<br>*\/<br>const fs = require(\"fs\")<br>fs.writeFile(__dirname + \".\/demo.txt\",'hello',(err)=&gt;{<br> &nbsp; &nbsp;if(err){<br> &nbsp;      console.log(\"\u5199\u5165\u5931\u8d25\") &nbsp; <br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"\u6587\u4ef6\u5199\u5165\u6210\u529f\")<br> &nbsp;  }<br>}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d41\u5f0f\u6587\u4ef6\u5199\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*<br>    1. \u521b\u5efa\u4e00\u4e2a\u6d41<br>    fs.createWriteStream(path[,option]){<br>        path : \u8981\u5199\u5165\u6587\u4ef6\u7684\u8def\u5f84+\u6587\u4ef6\u540d+\u6587\u4ef6\u540e\u7f00<br>        options : \u914d\u7f6e\u5bf9\u8c61<br>            flags<br>            encoding<br>            fd  \u6587\u4ef6\u7edf\u4e00\u6807\u8bc6\u7b26,linu\u4e0b\u6587\u4ef6\u6807\u8bc6\u7b26<br>            mode<br>            autoClose \u81ea\u52a8\u5173\u95ed -- \u6587\u4ef6 \u9ed8\u8ba4\u503c true<br>            emitClose \u5173\u95ed --- \u6587\u4ef6 \u9ed8\u8ba4\u503c false<br>            start : \u8bfb\u53d6\u6587\u4ef6\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u504f\u79fb\u91cf\uff09  <br>    }<br>*\/<br>let fs = require(\"fs\")<br>\/\/ \u521b\u5efa\u4e00\u4e2a\u53ef\u5199\u6d41<br>let ws = fs.createWriteStream(__dirname + '\/demo.txt')<br>\/\/ \u53ea\u8981\u7528\u6765\u6d41,\u5c31\u5fc5\u987b\u68c0\u6d4b\u7684\u5f62\u6001<br>ws.on('open',function(){<br> &nbsp; &nbsp;console.log(\"\u53ef\u5199\u6d41\u6253\u5f00\u4e86\")<br>})<br>ws.on('close',function(){<br> &nbsp; &nbsp;console.log('\u53ef\u5199\u6d41\u5173\u95ed\u4e86')<br>})<br>\/\/ \u4f7f\u7528\u53ef\u5199\u6d41\u5199\u5165\u6570\u636e<br>ws.write('hello world')<br>\/\/ \u5173\u95ed\u6d41<br>es.close() \/\/ \u5982\u679c\u5728Node\u76848\u7248\u672c\u4e2d,\u4f7f\u7528\u6b64\u65b9\u6cd5\u5173\u95ed\u6d41\u4f1a\u9020\u6210\u6570\u636e\u6d41\u5931<br>\/\/ \u5728node8 \u7248\u672c\u4e2d\u8981\u7528 ws.end() \u5173\u95ed<br>\u200b<br>\u200b<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>node\u4e2d\u7684\u6587\u4ef6\u7cfb\u7edf \u5728Node\u4e2d\u6709\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf,\u6240\u8c13\u7684\u6587\u4ef6\u7cfb\u7edf,\u5c31\u662f\u5bf9\u8ba1\u7b97\u673a\u4e2d\u6587\u4ef6\u8fdb\u884c\u589e\u5220\u6539\u67e5 \u5728node\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-node"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=188"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/188\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}