此URL.pathToFileURL函數將路徑轉換為文件,並確保在將給定路徑轉換為文件URL時正確添加/調整URL控製字符(/,\,:)。
用法:
url.pathToFileURL(path)
參數:該函數接受單個參數路徑,該路徑包含轉換文件URL的路徑。
返回值:此函數返回文件URL對象。
以下示例程序旨在說明URL.-pathToFileURL方法的使用:
範例1:
// Node program to demonstrate the
// URL.pathToFileURL API as Setter
// Importing the module 'url'
var url = require('url');
// Some random path from system
const path = 'D:\GeeksForGeeks'
// Converting the path to properly encoded file
console.log(url.pathToFileURL(path))
輸出:
URL { href:'file:///D:/GeeksForGeeks', origin:'null', protocol:'file:', username:'', password:'', host:'', hostname:'', port:'', pathname:'/D:/GeeksForGeeks', search:'', searchParams:URLSearchParams {}, hash:'' }
範例2:
// Node program to demonstrate the
// URL.pathToFileURL API as Setter
// Importing the module 'url'
var url = require('url');
// Some random path from system
const path = 'D:\NodeJS\node_modules\npm'
// Converting the path to properly encoded file
console.log(url.pathToFileURL(path))
輸出:
URL { href:'file:///D:/NodeJS%0Aode_modules%0Apm', origin:'null', protocol:'file:', username:'', password:'', host:'', hostname:'', port:'', pathname:'/D:/NodeJS%0Aode_modules%0Apm', search:'', searchParams:URLSearchParams {}, hash:'' }
注意:上麵的程序將使用node app.js命令編譯並運行。
相關用法
注:本文由純淨天空篩選整理自Abhishek7大神的英文原創作品 Node | URL.pathToFileURL API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。