>[success] # 內置模塊url
1. Node.js 的`url`模塊提供了一組用于解析和操作 URLs(統一資源定位符)的工具函數
* `url.parse()`:把 URL 字符串轉換成一個 URL 對象。
* `url.format()`:根據一個 URL 對象返回一個 URL 字符串。
* `url.resolve()`:類似于 Unix 中的`basename`,可以用來合并兩個 URL 字符串。
2. `url.parse()`將一個 URL 字符串解析成一個包含 URL 組件的對象。你可以訪問這些 URL 組件,例如`protocol`、`hostname`、`pathname`和`hash`。
~~~
const url = require('url');
const myUrl = 'https://github.com/nodejs/node/blob/v12.3.0/doc/api/url.md#url_url_parse_urlstring_parsequerystring_slashesdenotehost';
const parsedUrl = url.parse(myUrl);
console.log(parsedUrl);
// 輸出:Url {
// protocol: 'https:',
// slashes: true,
// auth: null,
// host: 'github.com',
// port: null,
// hostname: 'github.com',
// hash: '#url_url_parse_urlstring_parsequerystring_slashesdenotehost',
// search: null,
// query: null,
// pathname: '/nodejs/node/blob/v12.3.0/doc/api/url.md',
// path: '/nodejs/node/blob/v12.3.0/doc/api/url.md',
// href: 'https://github.com/nodejs/node/blob/v12.3.0/doc/api/url.md#url_url_parse_urlstring_parsequerystring_slashesdenotehost'
// }
~~~
>[info] ## 常見的api
| 方法名 | 描述 |
| --- | --- |
| `url.parse(urlString[, parseQueryString[, slashesDenoteHost]])` | 解析 URL 字符串并返回一個 URL 對象。parseQueryString 默認為`false`,表示是否將查詢參數解析為對象形式。slashesDenoteHost 默認為`false`,表示是否將兩個斜桿視為主機名的分隔符。 |
| `url.format(urlObject)` | 接收一個 URL 對象并返回相應的 URL 字符串。 |
| `url.resolve(from, to)` | 從`from`URL 解析出`to`URL 的絕對 URL。 |
| `url.URL` | URL 類。可以用來直接創建 URL 對象。 |
| `url.URLSearchParams` | URLSearchParams 類。可以用來操作 URL 查詢參數。 |
>[danger] ##### `url.parse(urlString[, parseQueryString[, slashesDenoteHost]])`
將 URL 字符串解析為 URL 對象。
* **urlString**:`<string>`,要解析的 URL 字符串。
* **parseQueryString**:`<boolean>`,可選參數,默認為 false。為 true 時表示將查詢字符串解析為對象。
* **slashesDenoteHost**:`<boolean>`,可選參數,默認為 false。為 true 時表示兩個斜桿視為主機名的分隔符。
返回`<URL>`對象。
~~~
const { URL } = require('url');
const myUrl = new URL('https://example.com/foo/bar?baz=qux');
console.log(myUrl);
// 輸出:
// URL {
// href: 'https://example.com/foo/bar?baz=qux',
// origin: 'https://example.com',
// protocol: 'https:',
// username: '',
// password: '',
// host: 'example.com',
// hostname: 'example.com',
// port: '',
// pathname: '/foo/bar',
// search: '?baz=qux',
// searchParams: URLSearchParams { 'baz' => 'qux' },
// hash: ''
// }
~~~
>[danger] ##### `url.format(urlObject)`
接收一個 URL 對象,返回相應的 URL 字符串。
* **urlObject**:`<URL>`,要轉換的 URL 對象。
返回`<string>`,表示 URL 字符串。
~~~
const { URL } = require('url');
const myUrl = new URL('https://example.com/foo/bar?baz=qux');
console.log(myUrl.href); // 輸出:https://example.com/foo/bar?baz=qux
const { format } = require('url');
console.log(format(myUrl)); // 輸出:https://example.com/foo/bar?baz=qux
~~~
>[danger] ##### `url.resolve(from, to)`
從`from`URL 解析出`to`URL 的絕對 URL。
* **from**:`<string>`,基本 URL,也就是相對 URL 解析時的上下文。
* **to**:`<string>`,要解析的目標 URL。
返回`<string>`,表示絕對 URL。
~~~
const { resolve } = require('url');
console.log(resolve('/foo/bar', './baz')); // 輸出:/foo/baz
console.log(resolve('/foo/bar', '/baz')); // 輸出:/baz
~~~
>[danger] ##### `url.URL`
URL 類。可以用來直接創建 URL 對象。
~~~
const { URL } = require('url');
const myUrl = new URL('https://example.com/foo/bar?baz=qux');
console.log(myUrl.protocol); // 輸出:https:
~~~
>[danger] ##### `url.URLSearchParams`
URLSearchParams 類。可以用來操作 URL 查詢參數。
~~~
const { URLSearchParams } = require('url');
const params = new URLSearchParams('foo=bar&baz=qux');
console.log(params.get('foo')); // 輸出:bar
console.log(params.get('baz')); // 輸出:qux
console.log(params.getAll('foo')); // 輸出:['bar']
console.log(params.toString()); // 輸出:foo=bar&baz=qux
~~~
- 基礎
- 什么是Node.js
- 理解 I/O 模型
- 理解node 中 I/O
- 對比node 和java 使用場景
- node 模塊管理
- 內置模塊 -- buffer
- 內置模塊 -- fs
- fs -- 文件描述符
- fs -- 打開文件 api
- fs -- 文件讀取 api
- fs -- 文件寫入 api
- fs -- 創建目錄 api
- fs -- 讀取文件目錄結構 api
- fs -- 文件狀態(信息) api
- fs -- 刪除文件/目錄 api
- fs -- 重命名 api
- fs -- 復制文件 api
- 內置模塊 -- events
- 內置模塊 -- stream
- 可讀流 -- Readable
- 可寫流 -- Writable
- Duplex
- Transform
- 內置模塊 -- http
- http -- 從客戶端發起
- http -- 從服務端發起
- 內置模塊 -- url
- 網絡開發