• Node.js Video Tutorials

NodeJS - url.parse() 方法



NodeJS url.parse() 方法接受一個 URL 字串,對其進行解析,最後返回一個包含提供的 URL 字串中各個片段的物件。

返回的物件包含以下屬性

  • protocol: 指定 URL 的協議方案(例如:https:)。

  • slashes: 一個布林值,指定協議後面是否跟著兩個 ASCII 正斜槓 (//)。

  • auth: 指定 URL 的身份驗證片段(例如:username:password)。

  • username: 指定身份驗證片段中的使用者名稱。

  • password: 指定身份驗證片段中的密碼。

  • host: 指定主機名和埠號。

  • hostname: 指定主機名,不包括埠號。

  • port: 指示埠號。

  • pathname: 指示 URL 路徑。

  • query: 包含已解析的查詢字串,除非 parsing 設定為 false。

  • hash: 指定 URL 的片段部分,包括“#”。

  • href: 指定完整的 URL。

  • origin: 指定 URL 的來源。

語法

以下是NodeJS url.parse() 方法的語法

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

引數

此方法接受三個引數。下面將對它們進行描述

  • urlString: 此引數指定需要解析的 URL 字串。

  • parseQueryString: 此引數指定一個布林值。如果為true,則 URL 字串中的查詢片段將設定為一個物件。如果為false,則 query 屬性將是一個未解析的字串。預設值為false

  • slashesDenoteHost: 此引數指定一個布林值。如果為true,則文字字串“//”之後且在下一個“/”之前的第一個標記將被視為主機。例如,考慮 (//one/two),結果將是 {host: ‘one’, pathname: ‘/two’} 而不是 {pathname: ‘//one/two’}。預設值為false

返回值

此方法獲取一個 URL 字串並返回一個物件。

  • 如果要解析的 urlString 不是字串,則此方法會丟擲 TypeError。

  • 如果 auth 屬性存在但無法解碼,則此方法會丟擲 URIError。

示例

如果我們將布林值true作為 NodeJS url.parse() 方法的parseQueryString引數傳遞,它會將 query 屬性設定為物件形式。

const url = require('url');
const address = 'https://user:pass@site.com:2000/pa/th?q=val#hash';
let result = url.parse(address, true);
console.log(result)

輸出

Url {
  protocol: 'https:',
  slashes: true,
  auth: 'user:pass',
  host: 'site.com:2000',
  port: '2000',
  hostname: 'site.com',
  hash: '#hash',
  search: '?q=val',
  query: { q: 'val' },
  pathname: '/pa/th',
  path: '/pa/th?q=val',
  href: 'https://user:pass@site.com:2000/pa/th?q=val#hash' 
}

示例

如果我們將布林值false作為 NodeJS parse() 方法的parseQueryString引數傳遞,它會將 query 屬性作為未解析的字串返回。

const url = require('url');
const address = 'https://site.com/pa/th?q=val#hash';
let result = url.parse(address, false);
console.log(result)

輸出

Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'site.com',
  port: null,
  hostname: 'site.com',
  hash: '#hash',
  search: '?q=val',
  query: 'q=val',
  pathname: '/pa/th',
  path: '/pa/th?q=val',
  href: 'https://site.com/pa/th?q=val#hash' 
}

示例

如果 parse() 方法的 urlString 引數不是字串,它會返回 TypeError。

這裡,我們嘗試解析一個不是字串的 URL 字串。

const url = require('url');
const address = 798032;
let result = url.parse(address, true);
console.log(result)

TypeError

正如我們在下面的輸出中看到的,由於提供的 URL 字串不是字串,parse() 方法丟擲了 TypeError。

url.js:150
    throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type number
    at Url.parse (url.js:150:11)
    at Object.urlParse [as parse] (url.js:144:13)
    at Object.<anonymous> (/home/cg/root/63aab5fe9c383/main.js:5:18)
    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)
urlobject Properties (Legacy)
nodejs_url_module.htm
廣告

© . All rights reserved.