1. 程式人生 > >nodejs——URL 基礎API

nodejs——URL 基礎API

node.js中文url API文件地址:http://nodejs.cn/api/url.html

以下是URL的基礎API的簡單使用:

 

url.parse(url)

將字串解析成url物件

基本的解析:

命令列輸入:

 1 $ url.parse('https://www.imooc.com/video/6710') 

列印如下:

Url {
protocol: 'https:', // 底層使用的協議
slashes: true, // 是否有協議的雙實線
auth: null, // 
host: 'www.imooc.com', //
ip或域名 port: null, // 是否有埠 hostname: 'www.imooc.com', // 主機名 hash: null, // 雜湊值(一般是錨點) search: null, // 查詢字串引數 query: null, // 傳送給http伺服器的資料 pathname: '/video/6710', // 訪問資源的路徑名 path: '/video/6710', // 訪問資源的路徑 href: 'https://www.imooc.com/video/6710' // 未解析的詳細的url地址 }
View Code

 

加上引數或錨點:

命令列輸入:

$ url.parse('https://www.imooc.com:8080/video/6710?from=mankii&course=node#floor1')

列印如下:

 1 Url {
 2 protocol: 'https:',
 3 slashes: true,
 4 auth: null,
 5 host: 'www.imooc.com:8080',
 6 port: '8080',
 7 hostname: 'www.imooc.com',
 8 hash: '#floor1',
 9 search: '?from=mankii&course=node',
10 query: 'from=mankii&course=node',
11 pathname: '/video/6710',
12 path: '/video/6710?from=mankii&course=node',
13 href: 'https://www.imooc.com:8080/video/6710?from=mankii&course=node#floor1' 14 }
View Code


url.format(options)

與url.parse()相對的,url.format將url物件轉換成字串

命令列輸入:

 1 $ url.format{
 2 protocol: 'https:',
 3 slashes: true,
 4 auth: null,
 5 host: 'www.imooc.com:8080',
 6 port: '8080',
 7 hostname: 'www.imooc.com',
 8 hash: '#floor1',
 9 search: '?from=mankii&course=node',
10 query: 'from=mankii&course=node',
11 pathname: '/video/6710',
12 path: '/video/6710?from=mankii&course=node',
13 href: 'https://www.imooc.com:8080/video/6710?from=mankii&course=node#floor1'
14 }
View Code

列印如下:

'https://www.imooc.com:8080/video/6710?from=mankii&course=node#floor1'
View Code

 

url.resolve(url,path)

以一種 Web 瀏覽器解析超連結的方式把一個目標 URL 解析成相對於一個基礎 URL

命令列輸入:

1 $ url.resolve("https://www.imooc.com","/course/list");

列印如下:

1 'https://www.imooc.com/course/list'

 

----- url.parse更多引數 ----


url.parse(url, true)

// 第二個引數為true時,則將query解析成物件格式

命令列輸入:

1 $ url.parse('https://www.imooc.com:8080/video/6710?from=mankii&course=node#floor1', true)

列印如下:

 1 Url {
 2 protocol: 'https:',
 3 slashes: true,
 4 auth: null,
 5 host: 'www.imooc.com:8080',
 6 port: '8080',
 7 hostname: 'www.imooc.com',
 8 hash: '#floor1',
 9 search: '?from=mankii&course=node',
10 query: { from: 'mankii', course: 'node' },
11 pathname: '/video/6710',
12 path: '/video/6710?from=mankii&course=node',
13 href: 'https://www.imooc.com:8080/video/6710?from=mankii&course=node#floor1' }
View Code

 


url.parse(url, true, true)

// 第三個引數為true時,會自動根據協議來解析

例如,第三個引數不設定時,命令列輸入:

1 $ url.parse("//imooc.com/course/list", true)

列印如下:可以看到無法正常解析

 1 Url {
 2 protocol: null,
 3 slashes: null,
 4 auth: null,
 5 host: null,
 6 port: null,
 7 hostname: null,
 8 hash: null,
 9 search: '',
10 query: {},
11 pathname: '//imooc.com/course/list
12 path: '//imooc.com/course/list',
13 href: '//imooc.com/course/list' }
View Code

 

加上第三個引數,命令列輸入:

1 $ url.parse("//imooc.com/course/list", true, true)

列印如下:

 1 Url {
 2 protocol: null,
 3 slashes: true,
 4 auth: null,
 5 host: 'imooc.com',
 6 port: null,
 7 hostname: 'imooc.com',
 8 hash: null,
 9 search: '',
10 query: {},
11 pathname: '/course/list',
12 path: '/course/list',
13 href: '//imooc.com/course/list' }
View Code