{"id":6148,"date":"2025-06-03T13:59:41","date_gmt":"2025-06-04T06:32:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6148"},"modified":"2025-06-04T13:59:41","modified_gmt":"2025-06-04T06:32:41","slug":"%e4%ba%86%e8%a7%a3node-js%e4%b8%ad%e7%9a%84%e8%b7%af%e5%be%91%e6%a8%a1%e5%a1%8a","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e4%ba%86%e8%a7%a3node-js%e4%b8%ad%e7%9a%84%e8%b7%af%e5%be%91%e6%a8%a1%e5%a1%8a\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Node.js\u7684\u8def\u5f91\u6a21\u7d44\uff1a\u63d0\u5347\u6587\u4ef6\u7ba1\u7406\u6548\u7387\u7684\u5be6\u7528\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, \u8def\u5f91\u6a21\u7d44, \u6587\u4ef6\u7ba1\u7406, \u7a0b\u5f0f\u7bc4\u4f8b, Web \u958b\u767c\"><\/p>\n<p>Node.js\u662f\u4e00\u500b\u5ee3\u53d7\u6b61\u8fce\u7684JavaScript\u5f8c\u7aef\u958b\u767c\u6846\u67b6\uff0c\u5176\u5f37\u5927\u7684\u529f\u80fd\u5438\u5f15\u4e86\u8a31\u591a\u958b\u767c\u8005\u3002\u5176\u4e2d\uff0c\u8def\u5f91\u6a21\u7d44\uff08`path`\uff09\u662f\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u9304\u7684\u91cd\u8981\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7c21\u55ae\u800c\u9748\u6d3b\u7684API\u4f86\u64cd\u4f5c\u6587\u4ef6\u8def\u5f91\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u61c9\u5c0d\u5404\u7a2e\u6587\u4ef6\u7ba1\u7406\u9700\u6c42\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u8def\u5f91\u6a21\u7d44\uff1f<\/h2>\n<p>\u8def\u5f91\u6a21\u7d44\u662fNode.js\u4e2d\u7684\u6838\u5fc3\u6a21\u7d44\u4e4b\u4e00\uff0c\u5c08\u9580\u7528\u65bc\u8655\u7406\u548c\u64cd\u4f5c\u6587\u4ef6\u8def\u5f91\u3002\u7121\u8ad6\u4f60\u662f\u5728\u69cb\u5efa\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0c\u5408\u7406\u4f7f\u7528\u8def\u5f91\u6a21\u7d44\u90fd\u80fd\u5927\u5e45\u63d0\u5347\u4f60\u7684\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p>### \u8def\u5f91\u6a21\u7d44\u7684\u4e3b\u8981\u529f\u80fd<\/p>\n<p>\u8def\u5f91\u6a21\u7d44\u63d0\u4f9b\u4e86\u591a\u7a2eAPI\u4f86\u8655\u7406\u6587\u4ef6\u8def\u5f91\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\nconst path = require(&#8216;path&#8217;);<\/p>\n<p>\/\/ \u5c07\u6587\u4ef6\u8def\u5f91\u8f49\u63db\u70ba\u7d55\u5c0d\u8def\u5f91<br \/>\nconst absolutePath = path.resolve(&#8216;\/foo\/bar&#8217;);<br \/>\nconsole.log(absolutePath); \/\/ \u8f38\u51fa\uff1a\/foo\/bar<\/p>\n<p>\/\/ \u5c07\u6587\u4ef6\u8def\u5f91\u8f49\u63db\u70ba\u76f8\u5c0d\u8def\u5f91<br \/>\nconst relativePath = path.relative(&#8216;\/foo\/bar&#8217;, &#8216;\/foo\/baz&#8217;);<br \/>\nconsole.log(relativePath); \/\/ \u8f38\u51fa\uff1a..\/baz<\/p>\n<p>\/\/ \u5c07\u6587\u4ef6\u8def\u5f91\u5206\u5272\u70ba\u76ee\u9304\u548c\u6587\u4ef6\u540d<br \/>\nconst dirname = path.dirname(&#8216;\/foo\/bar\/baz.txt&#8217;);<br \/>\nconsole.log(dirname); \/\/ \u8f38\u51fa\uff1a\/foo\/bar<\/p>\n<p>\/\/ \u5c07\u6587\u4ef6\u8def\u5f91\u5206\u5272\u70ba\u6587\u4ef6\u540d\u548c\u64f4\u5c55\u540d<br \/>\nconst basename = path.basename(&#8216;\/foo\/bar\/baz.txt&#8217;);<br \/>\nconsole.log(basename); \/\/ \u8f38\u51fa\uff1abaz.txt<\/p>\n<p>\/\/ \u5c07\u6587\u4ef6\u8def\u5f91\u5206\u5272\u70ba\u6587\u4ef6\u540d\u548c\u64f4\u5c55\u540d\uff0c\u4f46\u4e0d\u5305\u62ec\u64f4\u5c55\u540d<br \/>\nconst filename = path.parse(&#8216;\/foo\/bar\/baz.txt&#8217;).name;<br \/>\nconsole.log(filename); \/\/ \u8f38\u51fa\uff1abaz<\/p>\n<p>\/\/ \u5c07\u6587\u4ef6\u8def\u5f91\u5206\u5272\u70ba\u64f4\u5c55\u540d<br \/>\nconst extname = path.extname(&#8216;\/foo\/bar\/baz.txt&#8217;);<br \/>\nconsole.log(extname); \/\/ \u8f38\u51fa\uff1a.txt<br \/>\n&#8220;`<\/p>\n<p>\u8def\u5f91\u6a21\u7d44\u9084\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u7684\u5be6\u7528\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\n\/\/ \u5c07\u591a\u500b\u8def\u5f91\u62fc\u63a5\u70ba\u4e00\u500b\u8def\u5f91<br \/>\nconst joinedPath = path.join(&#8216;\/foo&#8217;, &#8216;bar&#8217;, &#8216;baz&#8217;);<br \/>\nconsole.log(joinedPath); \/\/ \u8f38\u51fa\uff1a\/foo\/bar\/baz<\/p>\n<p>\/\/ \u5c07\u8def\u5f91\u4e2d\u7684\u659c\u6760\u66ff\u63db\u70ba\u53cd\u659c\u6760<br \/>\nconst normalizedPath = path.normalize(&#8216;\/foo\/bar\/baz\/&#8217;);<br \/>\nconsole.log(normalizedPath); \/\/ \u8f38\u51fa\uff1a\/foo\/bar\/baz<\/p>\n<p>\/\/ \u5c07\u8def\u5f91\u4e2d\u7684\u53cd\u659c\u6760\u66ff\u63db\u70ba\u659c\u6760<br \/>\nconst unnormalizedPath = path.win32.normalize(&#8216;\\\\foo\\\\bar\\\\baz\\\\&#8217;);<br \/>\nconsole.log(unnormalizedPath); \/\/ \u8f38\u51fa\uff1a\\foo\\bar\\baz\\<\/p>\n<p>const parsedPath = path.parse(&#8216;\/foo\/bar\/baz.txt&#8217;);<br \/>\nconsole.log(parsedPath); \/\/ \u8f38\u51fa\uff1a{ root: &#8216;\/&#8217;, dir: &#8216;\/foo\/bar&#8217;, base: &#8216;baz.txt&#8217;, ext: &#8216;.txt&#8217;, name: &#8216;baz&#8217; }<br \/>\n&#8220;`<\/p>\n<p>### \u70ba\u4ec0\u9ebc\u8981\u4f7f\u7528\u8def\u5f91\u6a21\u7d44\uff1f<\/p>\n<p>\u4f7f\u7528\u8def\u5f91\u6a21\u7d44\u80fd\u5920\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u5b83\u8b93\u958b\u767c\u8005\u4e0d\u5fc5\u95dc\u5fc3\u64cd\u4f5c\u7cfb\u7d71\u7684\u5dee\u7570\uff0c\u7121\u8ad6\u662f\u5728Windows\u9084\u662fLinux\u4e0a\uff0c\u8def\u5f91\u7684\u64cd\u4f5c\u90fd\u80fd\u4fdd\u6301\u4e00\u81f4\u6027\u3002\u6b64\u5916\uff0c\u8def\u5f91\u6a21\u7d44\u7684\u591a\u7a2eAPI\u4f7f\u5f97\u6587\u4ef6\u7ba1\u7406\u8b8a\u5f97\u7c21\u55ae\u800c\u65b9\u4fbf\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\uff0c\u800c\u4e0d\u662f\u5e95\u5c64\u7684\u8def\u5f91\u8655\u7406\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cNode.js\u4e2d\u7684\u8def\u5f91\u6a21\u7d44\u662f\u958b\u767c\u8005\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u7121\u8ad6\u662f\u5728\u8655\u7406\u6587\u4ef6\u4e0a\u50b3\u3001\u4e0b\u8f09\uff0c\u9084\u662f\u6587\u4ef6\u7cfb\u7d71\u7684\u64cd\u4f5c\uff0c\u90fd\u80fd\u767c\u63ee\u51fa\u5176\u5de8\u5927\u7684\u4f5c\u7528\u3002\u5982\u4eca\uff0c\u96a8\u8457Node.js\u7684\u6301\u7e8c\u767c\u5c55\uff0c\u638c\u63e1\u8def\u5f91\u6a21\u7d44\u7684\u4f7f\u7528\u5c07\u70ba\u4f60\u7684\u958b\u767c\u5de5\u4f5c\u5e36\u4f86\u66f4\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<p>\u66f4\u65b0\u65e5\u671f\uff1a2025\/06\/01<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:Node.js\u7684\u8def\u5f91\u6a21\u584a\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684API\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u8655\u7406\u6587\u4ef6\u548c\u76ee\u9304\u7684\u8def\u5f91\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Node.js\u4e2d\u7684\u8def\u5f91\u6a21\u584a\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u793a\u4f8b\u4f86\u89e3\u91cb\u5982\u4f55\u4f7f\u7528\u5b83\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-6148","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-1Ba","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6148","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=6148"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6148\/revisions"}],"predecessor-version":[{"id":6149,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6148\/revisions\/6149"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}