Node.js之Path模組
阿新 • • 發佈:2020-12-30
Node.js path 模組提供了一些用於處理檔案路徑的小工具,我們可以通過以下方式引入該模組:
const path = require("path")
方法
1、path.join([path1][, path2][, ...]):
const path = require("path"); console.log(path.join(__dirname + "/index.html" + "/abc" + "\.../" + "123")); // G:\work\node\node1\index.html\abc...\123 console.log(path.join(__dirname + "/index.html" + "/abc" + "/.../" + "123")); //G:\work\node\node1\index.html\abc\...\123 console.log(path.join(__dirname + "/index.html" + "/abc" + "/../" + "123")); // G:\work\node\node1\index.html\123 console.log(path.join(__dirname + "/index.html" + "/abc" + "\../" + "123")); // G:\work\node\node1\index.html\abc..\123 console.log(path.join(__dirname + "/index.html" + "/abc" + "/./" + "123")); //G:\work\node\node1\index.html\abc\123 console.log(path.join(__dirname + "/index.html" + "/abc" + "\./" + "123")); // G:\work\node\node1\index.html\abc.\123
拼接方式:
1)先連線,連線時去掉“\”等特殊字元;
2)連線後處理"/../"和"/./"情況。