url有一个parse方法,这个方法返回一个json对象。
json对象里有两个常用的属性。
一个是query,表示请求的数据。
另一个是pathname,表示请求的地址。
const urlLib = require("url");
var obj = urlLib.parse("http://www.workplus.com/users?user=lily&pass=123");
/*Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.workplus.com',
port: null,
hostname: 'www.workplus.com', hash: null,
search: '?user=lily&pass=123',
query: 'user=lily&pass=123',
pathname: '/users',
path: '/users?user=lily&pass=123',
href: 'http://www.workplus.com/users?user=lily&pass=123' }*/
console.log(obj);
console.log(obj.query);//user=lily&pass=123
console.log(obj.pathname);///users
var obj = urlLib.parse("http://www.workplus.com/users?user=lily&pass=123",true);
/*Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.workplus.com',
port: null,
hostname: 'www.workplus.com',
hash: null,
search: '?user=lily&pass=123',
query: { user: 'lily', pass: '123' },
pathname: '/users',
path: '/users?user=lily&pass=123',
href: 'http://www.workplus.com/users?user=lily&pass=123' }*/
console.log(obj);
console.log(obj.query);//{ user: 'lily', pass: '123' }
console.log(obj.pathname);///users
网友评论