{"id":52909,"date":"2024-12-03T14:36:30","date_gmt":"2024-12-03T06:36:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52909\/"},"modified":"2024-12-03T14:36:30","modified_gmt":"2024-12-03T06:36:30","slug":"%e7%bc%93%e5%86%b2%e5%8c%ba%ef%bc%9anodejs","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52909\/","title":{"rendered":"\u7f13\u51b2\u533a\uff1aNodejs"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7f13\u51b2\u533a\uff1aNodejs<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7f13\u51b2\u533a\uff1aNodejs\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324397396742eebb08e53.jpg\" class=\"aligncenter\" title=\"\u7f13\u51b2\u533a\uff1aNodejs\u63d2\u56fe\" alt=\"\u7f13\u51b2\u533a\uff1aNodejs\u63d2\u56fe\" \/><\/p>\n<h3> node.js \u4e2d\u7f13\u51b2\u533a\u7684\u7b80\u5355\u6307\u5357 <\/h3>\n<p>node.js \u4e2d\u7684 <strong>buffer<\/strong> \u7528\u4e8e\u5904\u7406\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u8fd9\u5728\u5904\u7406\u6d41\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h3> \u5982\u4f55\u521b\u5efa\u7f13\u51b2\u533a <\/h3>\n<ol>\n<li> <strong>\u6765\u81ea\u5b57\u7b26\u4e32<\/strong>\uff1a<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.from('hello');\n\n\n<\/pre>\n<ol>\n<li> <strong>\u5206\u914d\u7279\u5b9a\u5927\u5c0f\u7684\u7f13\u51b2\u533a<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.alloc(10); \/\/ 10-byte buffer filled with zeros\n\n\n<\/pre>\n<ol>\n<li> <strong>\u6765\u81ea\u5b57\u8282\u6570\u7ec4<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.from([72, 101, 108, 108, 111]); \/\/ represents 'hello'\n\n\n<\/pre>\n<h3> \u91cd\u8981\u7684\u7f13\u51b2\u533a\u529f\u80fd <\/h3>\n<ol>\n<li> <strong>\u5c06\u7f13\u51b2\u533a\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.from('hello');\n   console.log(buf.tostring()); \/\/ 'hello'\n\n\n<\/pre>\n<ol>\n<li> <strong>\u83b7\u53d6\u7f13\u51b2\u533a\u957f\u5ea6<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.from('hello');\n   console.log(buf.length); \/\/ 5 (each character takes 1 byte)\n\n\n<\/pre>\n<ol>\n<li> <strong>\u5c06\u6570\u636e\u5199\u5165\u7f13\u51b2\u533a<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.alloc(5);\n   buf.write('hi');\n   console.log(buf.tostring()); \/\/ 'hi'\n\n\n<\/pre>\n<ol>\n<li> <strong>\u5bf9\u7f13\u51b2\u533a\u8fdb\u884c\u5207\u7247<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf = buffer.from('hello world');\n   const slice = buf.slice(0, 5);\n   console.log(slice.tostring()); \/\/ 'hello'\n\n\n<\/pre>\n<ol>\n<li> <strong>\u4ece\u4e00\u4e2a\u7f13\u51b2\u533a\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u7f13\u51b2\u533a<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf1 = buffer.from('hello');\n   const buf2 = buffer.alloc(5);\n   buf1.copy(buf2);\n   console.log(buf2.tostring()); \/\/ 'hello'\n\n\n<\/pre>\n<ol>\n<li> <strong>\u6bd4\u8f83\u4e24\u4e2a\u7f13\u51b2\u533a<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf1 = buffer.from('abc');\n   const buf2 = buffer.from('abc');\n   console.log(buf1.equals(buf2)); \/\/ true\n\n\n<\/pre>\n<ol>\n<li> <strong>\u8fde\u63a5\u591a\u4e2a\u7f13\u51b2\u533a<\/strong>:<\/li>\n<\/ol>\n<pre>\n\n   const buf1 = Buffer.from('Hello');\n   const buf2 = Buffer.from(' World');\n   const buf3 = Buffer.concat([buf1, buf2]);\n   console.log(buf3.toString()); \/\/ 'Hello World'\n\n\n<\/pre>\n<p>\u8fd9\u4e9b\u662f\u5f00\u59cb\u5728 node.js \u4e2d\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\u9700\u8981\u4e86\u89e3\u7684\u5173\u952e buffer \u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u3001\u5199\u5165\u548c\u8bfb\u53d6\u7f13\u51b2\u533a<\/li>\n<li>\u5207\u7247\u3001\u590d\u5236\u3001\u6bd4\u8f83\u548c\u8fde\u63a5\u7f13\u51b2\u533a<\/li>\n<\/ul>\n<p>\u8fd9\u8db3\u4ee5\u5904\u7406 node.js \u4e2d\u7684\u5927\u591a\u6570\u521d\u5b66\u8005\u7528\u4f8b\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u9762\u5411\u4ee3\u7801\u8bed\u8a00\u6a21\u578b\u7684\u5b89\u5168\u6027\u7814\u7a76\u5168\u65b0\u8fdb\u5c55\uff0c\u5357\u5927&amp;NTU\u8054\u5408\u53d1\u5e03\u5168\u9762\u7efc\u8ff0\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u7f13\u51b2\u533a\uff1aNodejs \u6536\u85cf \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u7f13\u51b2\u533a\uff1aNodejs\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 node.js \u4e2d\u7f13\u51b2\u533a\u7684\u7b80\u5355\u6307\u5357 node.js \u4e2d\u7684 buffer \u7528\u4e8e\u5904\u7406\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u8fd9\u5728\u5904\u7406\u6d41\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002 \u5982\u4f55\u521b\u5efa\u7f13\u51b2\u533a \u6765\u81ea\u5b57\u7b26\u4e32\uff1a const buf = buffer.from(&#8216;hello&#8217;); \u5206\u914d\u7279\u5b9a\u5927\u5c0f\u7684\u7f13\u51b2\u533a: const buf = buffer.alloc(10); \/\/ 10-byte buffer filled with zeros \u6765\u81ea\u5b57\u8282\u6570\u7ec4: const buf = buffer.from([72, 101, 108, 108, 111]); \/\/ represents &#8216;hello&#8217; \u91cd\u8981\u7684\u7f13\u51b2\u533a\u529f\u80fd \u5c06\u7f13\u51b2\u533a\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32: const buf = buffer.from(&#8216;hello&#8217;); console.log(buf.tostring()); \/\/ &#8216;hello&#8217; \u83b7\u53d6\u7f13\u51b2\u533a\u957f\u5ea6: const buf = buffer.from(&#8216;hello&#8217;); console.log(buf.length); \/\/ 5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52909","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52909","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=52909"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52909\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}