url 模块

url 模块用于处理与解析 URL

引入

1
const url = require('url');

方法

parse

解析网址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var url = require("url");
console.log(url.parse("https://www.google.com/search?q=%E7%8E%8B%E5%9D%9A&oq=%E7%8E%8B%E5%9D%9A&aqs=chrome"+
"..69i57j0l7.8770j0j8&sourceid=chrome&ie=UTF-8", true));
//返回
//Url {
// protocol: 'https:',
// slashes: true,
// auth: null,
// host: 'www.google.com',
// port: null,
// hostname: 'www.google.com',
// hash: null,
// search: '?q=%E7%8E%8B%E5%9D%9A&oq=%E7%8E%8B%E5%9D%9A&aqs=chrome..69i57j0l7.8770j0j8&sourceid=chrome&ie=UTF-8',
// query: [Object: null prototype] {
// q: '王坚',
// oq: '王坚',
// aqs: 'chrome..69i57j0l7.8770j0j8',
// sourceid: 'chrome',
// ie: 'UTF-8'
// },
// pathname: '/search',
// path: '/search?q=%E7%8E%8B%E5%9D%9A&oq=%E7%8E%8B%E5%9D%9A&aqs=chrome..69i57j0l7.8770j0j8&sourceid=chrome&ie=UTF-8',
// href: 'https://www.google.com/search?q=%E7%8E%8B%E5%9D%9A&oq=%E7%8E%8B%E5%9D%9A&aqs=chrome..69i57j0l7.8770j0j8&sourceid=chrome&ie=UTF-8'
//}