1.引入模块
const url=require('url');
2.重要API
url.parse(urlStr)
其中两个参数,第一个为url地址,第二个为true则将query解析为对象。
a. url.parse(urlStr, true)
let str='http://www.bing.com:8080/a/b/1.html?a=1&b=2&c=3';
console.log(url.parse(str, true));
如下query被解析成对象形式: query: { a: '1', b: '2', c: '3' }
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.bing.com:8080',
port: '8080',
hostname: 'www.bing.com',
hash: null,
search: '?a=1&b=2&c=3',
query: { a: '1', b: '2', c: '3' },
pathname: '/a/b/1.html',
path: '/a/b/1.html?a=1&b=2&c=3',
href: 'http://www.bing.com:8080/a/b/1.html?a=1&b=2&c=3'
}
b. url.parse(urlStr)
const url=require('url');
let str='http://www.bing.com:8080/a/b/1.html?a=1&b=2&c=3';
console.log(url.parse(str));
如下query未被解析对象形式: query: 'a=1&b=2&c=3'
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.bing.com:8080',
port: '8080',
hostname: 'www.bing.com',
hash: null,
search: '?a=1&b=2&c=3',
query: 'a=1&b=2&c=3',
pathname: '/a/b/1.html',
path: '/a/b/1.html?a=1&b=2&c=3',
href: 'http://www.bing.com:8080/a/b/1.html?a=1&b=2&c=3'
}