
- 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 排序 (Order By)
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢 (Join)
- 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 資料限制 (Limit)
- Node.js - MongoDB 連線查詢 (Join)
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - url.toString() 方法
URL 類 的 NodeJS url.toString() 方法 將返回 URL 物件的序列化 URL。此方法的返回值等效於 URL.href 屬性和 URL.toJSON() 方法。如果為 toString() 方法賦值的值不是有效的 URL,則會丟擲 TypeError。
Node.js URL 模組提供了多個用於 URL 解析和解析的實用程式,toString() 方法就是其中之一。
語法
以下是 URL 類 的 NodeJS toString() 方法 的語法
URL.toString()
引數
此方法不接受任何引數。
返回值
此方法在 URL 物件上返回序列化的 URL。
示例
如果我們嘗試為 NodeJS url.toString() 方法賦值一個有效的 URL,它將返回一個序列化 URL 字串。
在下面的示例中,我們嘗試從 URL 物件獲取序列化的 URL。
const url = require('node:url'); const myURL = new URL("https://tutorialspoint.tw/index.htm"); var result = myURL.toString(); console.log("The serialized URL: " + result);
輸出
執行上述程式後,url.toString() 方法將返回序列化的 URL 字串。
The serialized URL: https://tutorialspoint.tw/index.htm
示例
如果我們為 href 屬性和 toString() 方法分配一個有效的 URL,則兩者的返回值是等效的。
在下面的示例中,我們將一個有效的 URL 分配給 href 屬性和 toString 方法,並檢查兩者是否等效。
const url = require('node:url'); const myURL = new URL("https://tutorialspoint.tw/index.htm"); var toString = myURL.toString(); console.log("Result of toString property: " + toString); var href = myURL.href; console.log("Result of href property: " + href); if(toString === href){ console.log("Both toString and href results are same..."); } else { console.log("Both are not same..."); }
輸出
正如我們在下面的輸出中看到的,返回值相同。
Result of toString property: https://tutorialspoint.tw/index.htm Result of href property: https://tutorialspoint.tw/index.htm Both toString and href results are same...
示例
如果我們賦值的值不是有效的 URL 型別,則 toString() 方法將丟擲 TypeError。
在給定的程式中,我們為 toString 方法分配了一個無效的 URL。
const url = require('node:url'); const myURL = new URL("234httttttqerps://www.tutorialspoint.cgfom/index.htm"); var result = myURL.toString(); console.log("The serialized URL: " + result);
TypeError
如果我們編譯並執行上述程式,則 toString() 方法會丟擲 TypeError,因為分配的 URL 無效。
node:internal/url:564 throw new ERR_INVALID_URL(input); ^ TypeError [ERR_INVALID_URL]: Invalid URL at new NodeError (node:internal/errors:387:5) at URL.onParseError (node:internal/url:564:9) at new URL (node:internal/url:640:5) at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:3:15) 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 { input: '234httttttqerps://www.tutorialspoint.cgfom/index.htm', code: 'ERR_INVALID_URL' }
nodejs_url_module.htm
廣告