url.pathToFileURL(path)
添加於:v10.12.0
參數
此函數確保path
絕對解析,並且在轉換為文件 URL 時正確編碼 URL 控製字符。
import { pathToFileURL } from 'node:url'; new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
const { pathToFileURL } = require('node:url'); new URL(__filename); // Incorrect: throws (POSIX) new URL(__filename); // Incorrect: C:\... (Windows) pathToFileURL(__filename); // Correct: file:///... (POSIX) pathToFileURL(__filename); // Correct: file:///C:/... (Windows) new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
相關用法
- Node.js url.parse(urlString, parseQueryString, slashesDenoteHost)用法及代碼示例
- Node.js url.domainToUnicode(domain)用法及代碼示例
- Node.js url.domainToASCII(domain)用法及代碼示例
- Node.js url.urlToHttpOptions(url)用法及代碼示例
- Node.js url.resolve(from, to)用法及代碼示例
- Node.js url.format(URL[, options])用法及代碼示例
- Node.js url.fileURLToPath(url)用法及代碼示例
- Node.js url.format(urlObject)用法及代碼示例
- Node.js url.domainToUnicode()用法及代碼示例
- Node.js url.toString()用法及代碼示例
- Node.js urlObject.search用法及代碼示例
- Node.js URLSearchParams urlSearchParams[Symbol.iterator]()用法及代碼示例
- Node.js urlObject.auth用法及代碼示例
- Node.js urlObject.path用法及代碼示例
- Node.js urlObject.port用法及代碼示例
- Node.js urlSearchParams.values()用法及代碼示例
- Node.js urlObject.href用法及代碼示例
- Node.js urlObject.host用法及代碼示例
- Node.js urlObject.auth()用法及代碼示例
- Node.js urlObject.hash用法及代碼示例
- Node.js urlSearchParams.toString()用法及代碼示例
- Node.js urlObject.query用法及代碼示例
- Node.js urlObject.pathname用法及代碼示例
- Node.js urlObject.slashes用法及代碼示例
- Node.js urlObject.protocol用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 url.pathToFileURL(path)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。