
- 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 Where 條件
- 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 - 討論
Node.js - path.join() 方法
Node.js 的 path.join() 方法屬於 path 模組,它接受一系列的 字串路徑片段。該方法會使用平臺分隔符將所有給定的 字串路徑片段連線在一起,然後規範化完整的路徑。
長度為零的路徑片段將被忽略。如果連線後的 路徑字串長度為零,則會列印“.”。如果任何路徑片段不是 字串值,則會丟擲 TypeError。
語法
以下是 path 模組中 Node.js path.join() 方法的語法:
path.join([...paths])
引數
...paths − 此引數包含用逗號分隔的一系列路徑片段。
返回值
此方法透過連線 ...paths 引數中給定的所有 字串路徑片段,返回一個完整的 字串路徑。
以下示例演示了 path 模組中 path.join() 方法的使用。
示例
path.join() 方法以一系列 字串路徑片段作為輸入,並輸出一個完整的 字串路徑。
在下面的示例中,我們將一系列 字串路徑片段傳遞給 path.join() 方法。
const path = require('path'); var result = path.join('Users', 'Node.js', 'API', 'Path module'); console.log(result);
輸出
如果我們編譯並執行上述程式,此方法會將所有給定的路徑片段使用平臺特定的分隔符作為分隔符連線在一起,並列印完整的路徑。
Users/Node.js/API/Path module
示例
path.join() 方法接受一系列 字串路徑片段,並將其作為完整的 字串路徑返回。
在以下示例中,我們嘗試將一個 字串路徑片段 ("Nodefile.js") 與當前工作目錄連線起來。我們可以使用 '__dirname' 屬性獲取當前工作目錄。
const path = require('path'); var result = path.join(__dirname, "Nodefile.js"); console.log('Current working directory: ' + __dirname); console.log("After the joining a segment: " + result);
輸出
在線上編譯器 (POSIX) 中執行上述程式後,此方法會將 字串路徑片段 ("Nodefile.js") 與當前工作目錄連線起來。
Current working directory: /home/cg/root/63a028ab0650d After the joining a segment: /home/cg/root/63a028ab0650d/Nodefile.js
以下是在 Windows 作業系統上執行上述程式碼時的輸出。
Current working directory: C:\Users\Lenovo\Desktop\JavaScript After the joining a segment: C:\Users\Lenovo\Desktop\JavaScript\Nodefile.js
示例
如果我們將不是 字串型別的值傳遞給 …paths 引數,則該方法將丟擲 TypeError。
在以下示例中,我們將一個物件而不是字串傳遞給 path.isAbsolute() 方法的 …paths 引數。
const path = require('path'); var result = path.join({}); console.log( result);
輸出
正如我們在下面的輸出中看到的,path.isAbsolute() 方法丟擲了 TypeError,因為 …paths 引數不是字串值。
path.js:39 throw new ERR_INVALID_ARG_TYPE('path', 'string', path); ^ TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object at assertPath (path.js:39:11) at Object.join (path.js:1157:7) at Object.<anonymous> (/home/cg/root/63a028ab0650d/main.js:3:19) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19)