{"id":6322,"date":"2023-01-07T12:37:15","date_gmt":"2023-01-07T04:37:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6322"},"modified":"2023-01-07T12:37:15","modified_gmt":"2023-01-07T04:37:15","slug":"%e4%ba%86%e8%a7%a3node-js%e4%b8%adpath%e6%a8%a1%e5%9d%97%e7%9a%84%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%91%e8%99%95%e7%90%86","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e4%ba%86%e8%a7%a3node-js%e4%b8%adpath%e6%a8%a1%e5%9d%97%e7%9a%84%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%91%e8%99%95%e7%90%86\/","title":{"rendered":"\u4e86\u89e3Node.js\u4e2dPath\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f91\u8655\u7406"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, Path, \u6587\u4ef6\u8def\u5f91, \u6a21\u7d44\"><\/p>\n<h1>\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;path&#8217; \u6a21\u5757\u5904\u7406\u6587\u4ef6\u8def\u5f91\uff1f<\/h1>\n<p>Node.js \u662f\u4e00\u500b\u5ee3\u53d7\u6b61\u8fce\u7684 JavaScript \u74b0\u5883\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u958b\u767c\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\uff0c\u5305\u62ec\u7db2\u7ad9\u548c\u4f3a\u670d\u5668\u3002Node.js \u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u6a21\u7d44\u7cfb\u7d71\uff0c\u5176\u4e2d\u5305\u62ec &#8216;path&#8217; \u6a21\u7d44\uff0c\u53ef\u4ee5\u7528\u65bc\u8655\u7406\u6587\u4ef6\u8def\u5f91\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;path&#8217; \u6a21\u7d44\u4f86\u8655\u7406\u6587\u4ef6\u8def\u5f91\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f &#8216;path&#8217; \u6a21\u7d44\uff1f<\/h2>\n<p>&#8216;path&#8217; \u6a21\u7d44\u662f Node.js \u4e2d\u7684\u4e00\u500b\u5167\u7f6e\u6a21\u7d44\uff0c\u53ef\u4ee5\u7528\u65bc\u8655\u7406\u6587\u4ef6\u8def\u5f91\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u65bc\u64cd\u4f5c\u6587\u4ef6\u8def\u5f91\uff0c\u4f8b\u5982\uff1a\u89e3\u6790\u6587\u4ef6\u8def\u5f91\u3001\u62fc\u63a5\u6587\u4ef6\u8def\u5f91\u3001\u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7b49\u7b49\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 &#8216;path&#8217; \u6a21\u7d44\uff1f<\/h2>\n<p>\u8981\u4f7f\u7528 &#8216;path&#8217; \u6a21\u7d44\uff0c\u9996\u5148\u9700\u8981\u5c0e\u5165\u6a21\u7d44\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst path = require('path');\n<\/pre>\n<p>\u7136\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 &#8216;path&#8217; \u6a21\u7d44\u63d0\u4f9b\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\u6587\u4ef6\u8def\u5f91\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>path.join()<\/strong>\uff1a\u7528\u65bc\u62fc\u63a5\u6587\u4ef6\u8def\u5f91\uff1b<\/li>\n<li><strong>path.resolve()<\/strong>\uff1a\u7528\u65bc\u89e3\u6790\u6587\u4ef6\u8def\u5f91\uff1b<\/li>\n<li><strong>path.extname()<\/strong>\uff1a\u7528\u65bc\u7372\u53d6\u6587\u4ef6\u7684\u64f4\u5c55\u540d\uff1b<\/li>\n<li><strong>path.basename()<\/strong>\uff1a\u7528\u65bc\u7372\u53d6\u6587\u4ef6\u7684\u57fa\u672c\u540d\u7a31\uff1b<\/li>\n<li><strong>path.dirname()<\/strong>\uff1a\u7528\u65bc\u7372\u53d6\u6587\u4ef6\u7684\u76ee\u9304\u540d\u7a31\uff1b<\/li>\n<li><strong>path.exists()<\/strong>\uff1a\u7528\u65bc\u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>path.join()<\/strong> \u65b9\u6cd5\u4f86\u62fc\u63a5\u6587\u4ef6\u8def\u5f91\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst filePath = path.join('\/home', 'user', 'test.txt');\nconsole.log(filePath); \/\/ '\/home\/user\/test.txt'\n<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 <strong>path.resolve()<\/strong> \u65b9\u6cd5\u4f86\u89e3\u6790\u6587\u4ef6\u8def\u5f91\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst filePath = path.resolve('\/home', 'user', 'test.txt');\nconsole.log(filePath); \/\/ '\/home\/user\/test.txt'\n<\/pre>\n<p>\u6b64\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 <strong>path.exists()<\/strong> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst filePath = '\/home\/user\/test.txt';\npath.exists(filePath, (exists) => {\n    if (exists) {\n        console.log('\u6587\u4ef6\u5b58\u5728');\n    } else {\n        console.log('\u6587\u4ef6\u4e0d\u5b58\u5728');\n    }\n});\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;path&#8217; \u6a21\u7d44\u4f86\u8655\u7406\u6587\u4ef6\u8def\u5f91\u3002&#8217;path&#8217; \u6a21\u7d44\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u65bc\u64cd\u4f5c\u6587\u4ef6\u8def\u5f91\uff0c\u4f8b\u5982\uff1a\u89e3\u6790\u6587\u4ef6\u8def\u5f91\u3001\u62fc\u63a5\u6587\u4ef6\u8def\u5f91\u3001\u6aa2\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7b49\u7b49\u3002<br \/>\n<!--more--><br \/>\n\u4f7f\u7528 &#8216;path&#8217; \u6a21\u7d44\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u8655\u7406\u6587\u4ef6\u8def\u5f91\uff0c\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:Node.js\u4e2d\u7684Path\u6a21\u5757\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u8655\u7406\u6587\u4ef6\u8def\u5f91\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Path\u6a21\u5757\u4f86\u8655\u7406\u6587\u4ef6\u8def\u5f91\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u63d0\u9ad8\u958b\u767c\u6548\u7387\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-6322","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-1DY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6322","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=6322"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6322\/revisions"}],"predecessor-version":[{"id":6323,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6322\/revisions\/6323"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}