{"id":31513,"date":"2024-11-25T12:01:23","date_gmt":"2024-11-25T04:01:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31513\/"},"modified":"2024-11-25T12:01:23","modified_gmt":"2024-11-25T04:01:23","slug":"%e5%88%a9%e7%94%a8node-js%e8%bf%9b%e8%a1%8c%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e6%a0%bc%e5%bc%8f%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31513\/","title":{"rendered":"\u5229\u7528Node.js\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5229\u7528\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\uff0c\u5229\u7528Node.js\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">ASCII\u7f16\u7801\u5c31\u6bd4\u8f83\u86cb\u75bc\uff0c\u901a\u8fc7\u641c\u7d22\u7f51\u4e0a\u8d44\u6e90\uff0c\u53cd\u590d\u6d4b\u8bd5\u5bf9\u6bd4\uff0c\u6700\u7ec8\u5f62\u6210\u4e0b\u9762\u6bd4\u8f83\u9760\u8c31\u7684\u65b9\u6cd5\uff08\u6709\u4e00\u4e9b EditPlus\u663e\u793a\u7f16\u7801\u4e3autf-8\u4f46node.js\u5e93\u8fd4\u56de\u7684\u5374\u662f\u5176\u5b83\u7f16\u7801&gt;_<\/p>\n<p style=\"text-align: left\">\u5224\u65ad\u4fee\u6539\u662f\u5426\u65e0\u8bef\uff0c\u53ea\u9700\u8981\u5728\u4fee\u6539\u5b8c\u4e4b\u540e\uff0c\u901a\u8fc7SVN\u63d0\u4ea4\uff0c\u6d4f\u89c8\u63d0\u4ea4\u5217\u8868\uff0c\u53cc\u51fb\u4efb\u610f\u4e00\u9879\u5f85\u63d0\u4ea4\u6587\u4ef6\uff0c\u5982\u679c\u663e\u793a\u4e0b\u56fe\u6240\u793a\u7684\u5bf9\u8bdd\u6846\uff0c\u5219\u8bf4\u660e\u4fee\u6539\u6210\u529f\uff0c\u5176\u5b83\u90fd\u4f1a\u770b\u5230\u4e2d\u6587\u53cd\u800c\u53d8\u6210\u4e71\u7801\u4e86<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/5c352300c3cbb7bdee5b142cc24db2c0-0.png\" class=\"aligncenter\" title=\"\u5229\u7528Node.js\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\u63d2\u56fe\" alt=\"\u5229\u7528Node.js\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\u63d2\u56fe\" \/><\/p>\n<pre>var fs = require('fs');\r\nvar chardet = require('chardet');\r\nvar jschardet = require(\"jschardet\");\r\nvar encoding = require(\"encoding\");\r\nvar path = \"lua\u76ee\u5f55\";\r\nfunction readDirectory(dirPath) {\r\n  if (fs.existsSync(dirPath)) {\r\n    var files = fs.readdirSync(dirPath);\r\n    files.forEach(function (file) {\r\n      var filePath = dirPath + \"\/\" + file;\r\n      var stats = fs.statSync(filePath);\r\n      if (stats.isDirectory()) {\r\n        \/\/ console.log('\/n\u8bfb\u53d6\u76ee\u5f55\uff1a\n', filePath, \"\n\");\r\n        readDirectory(filePath);\r\n      } else if (stats.isFile() &amp;&amp; \/.lua$\/.test(filePath)) {\r\n        var buff = fs.readFileSync(filePath);\r\n        if (buff.length &amp;&amp; buff[0].toString(16).toLowerCase() == \"ef\" &amp;&amp; buff[1].toString(16).toLowerCase() == \"bb\" &amp;&amp; buff[2].toString(16).toLowerCase() == \"bf\") {\r\n          \/\/EF BB BF 239 187 191\r\n          console.log('\n\u53d1\u73b0BOM\u6587\u4ef6\uff1a', filePath, \"\n\");\r\n          buff = buff.slice(3);\r\n          fs.writeFile(filePath, buff.toString(), \"utf8\");\r\n        }\r\n        \/\/ { encoding: 'UTF-8', confidence: 0.99 }\r\n        \/\/ var charset = chardet.detectFileSync(filePath);\r\n        var info = jschardet.detect(buff);\r\n        if (info.encoding == \"GB2312\" || info.encoding == \"ascii\") {\r\n          var resultBuffer = encoding.convert(buff, \"UTF-8\", info.encoding);\r\n          fs.writeFile(filePath, resultBuffer, \"utf8\");\r\n        }\r\n        else if (info.encoding != \"UTF-8\" &amp;&amp; chardet.detectFileSync(filePath) != \"UTF-8\")\r\n        {\r\n          if (buff.toString().indexOf(\"\r\n\") &gt; -1)\r\n          {\r\n            var resultBuffer = encoding.convert(buff, \"UTF-8\", \"GBK\");\r\n            fs.writeFile(filePath, resultBuffer, \"utf8\");\r\n          }\r\n        }\r\n      }\r\n    });\r\n  } else {\r\n    console.log('Not Found Path : ', dirPath);\r\n  }\r\n}\r\nreadDirectory(path);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u6ce8\u610f\u4e0a\u9762\u7684\u5224\u65ad\uff0c\u7b2c\u4e00\u4e2a\u660e\u786e\u662f GB2312\u6216\u8005ascii\u65f6\uff0c\u76f4\u63a5\u5c06\u76f8\u5e94\u7684\u7f16\u7801\u8f6c\u4e3a utf-8\u3002\u800c\u5982\u679c\u8fd4\u56de\u662f\u683c\u5f0f\uff0c\u5148\u5224\u65ad\u662f\u5426\u6709PC\u4e0b\u7684\u6362\u884c\u7b26\uff0c\u5982\u679c\u6709\u5219\u5168\u90e8\u5c06\u5b83\u89c6\u4e3aGBK\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p style=\"text-align: left\">\u6574\u4e2a\u601d\u8def\u5176\u5b9e\u662f\u6bd4\u8f83\u7b80\u5355\uff0c\u96be\u70b9\u5728\u4e8e\u5982\u679c\u5224\u65ad\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u3002\u8fd9\u4e2a\u771f\u7684\u5f88\u96be&gt;_<\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p style=\"text-align: left\"><\/p>\n<p style=\"text-align: left\"><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528Node.js\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5229\u7528\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\uff0c\u5229\u7528Node.js\u8fdb\u884c\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 ASCII\u7f16\u7801\u5c31\u6bd4\u8f83\u86cb\u75bc\uff0c\u901a\u8fc7\u641c\u7d22\u7f51\u4e0a\u8d44\u6e90\uff0c\u53cd\u590d\u6d4b\u8bd5\u5bf9\u6bd4\uff0c\u6700\u7ec8\u5f62\u6210\u4e0b\u9762\u6bd4\u8f83\u9760\u8c31\u7684\u65b9\u6cd5\uff08\u6709\u4e00\u4e9b EditPlus\u663e\u793a\u7f16\u7801\u4e3autf-8\u4f46node.js\u5e93\u8fd4\u56de\u7684\u5374\u662f\u5176\u5b83\u7f16\u7801&gt;_ \u5224\u65ad\u4fee\u6539\u662f\u5426\u65e0\u8bef\uff0c\u53ea\u9700\u8981\u5728\u4fee\u6539\u5b8c\u4e4b\u540e\uff0c\u901a\u8fc7SVN\u63d0\u4ea4\uff0c\u6d4f\u89c8\u63d0\u4ea4\u5217\u8868\uff0c\u53cc\u51fb\u4efb\u610f\u4e00\u9879\u5f85\u63d0\u4ea4\u6587\u4ef6\uff0c\u5982\u679c\u663e\u793a\u4e0b\u56fe\u6240\u793a\u7684\u5bf9\u8bdd\u6846\uff0c\u5219\u8bf4\u660e\u4fee\u6539\u6210\u529f\uff0c\u5176\u5b83\u90fd\u4f1a\u770b\u5230\u4e2d\u6587\u53cd\u800c\u53d8\u6210\u4e71\u7801\u4e86 var fs = require(&#8216;fs&#8217;); var chardet = require(&#8216;chardet&#8217;); var jschardet = require(&#8220;jschardet&#8221;); var encoding = require(&#8220;encoding&#8221;); var path = &#8220;lua\u76ee\u5f55&#8221;; function readDirectory(dirPath) { if (fs.existsSync(dirPath)) { var files = fs.readdirSync(dirPath); files.forEach(function (file) { var filePath = dirPath + &#8220;\/&#8221; + file; var stats = fs.statSync(filePath); if (stats.isDirectory()) { \/\/ console.log(&#8216;\/n\u8bfb\u53d6\u76ee\u5f55\uff1a &#8216;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31513","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31513"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31513\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}