
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 快速入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 快速入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - fileURLToPath(url) 方法
URL 類中的NodeJS url.fileURLToPath() 方法接受檔案 URL 字串或 URL 物件,並將其轉換為正確編碼的路徑。此方法將確保百分比編碼的字元被完全解碼。Node.js 的URL 模組提供了各種用於 URL 解析和分析的實用程式。
語法
以下是URL 類中 NodeJS url.fileURLToPath() 方法的語法
url.fileURLToPath(url)
引數
url: 此引數指定將轉換為路徑的檔案 URL 字串或 URL 物件。
返回值
此方法返回一個完全解析的、特定於平臺的 Node.js 檔案路徑。
示例
如果將檔案 URL 字串傳遞給 NodeJS url.fileURLToPath() 方法,它將轉換為完全解析的、特定於平臺的路徑。
在下面的示例中,我們將檔案 URL 字串傳遞給 NodeJS url.fileURLToPath() 方法。
const { fileURLToPath } = require('node:url'); let FtoP = fileURLToPath('file:///C:/Desktop/file/'); console.log(FtoP);
輸出
執行上述程式後,將生成以下輸出
C:\Desktop\file\
示例
在 Windows 系統上,如果檔案 URL 字串包含非 ASCII 字元,則 fileURLToPath() 方法無法將其轉換為完全解析的路徑,並返回 TypeError。
在下面的示例中,我們將包含日語字元的檔案 URL 字串傳遞給 fileURLToPath() 方法。
const { fileURLToPath } = require('node:url'); let FtoP = fileURLToPath('file:///こんにちは:/'); console.log(FtoP);
TypeError
執行上述程式後,將生成以下輸出
node:internal/url:1407 throw new ERR_INVALID_FILE_URL_PATH('must be absolute'); ^ TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute at new NodeError (node:internal/errors:387:5) at getPathFromURLWin32 (node:internal/url:1407:11) at fileURLToPath (node:internal/url:1437:22) at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:3:12) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 { code: 'ERR_INVALID_FILE_URL_PATH' }
nodejs_url_module.htm
廣告