var url = require('url');
var qs = require('querystring');
var href = 'http://user:pass@host.com:8080/p/a/t/h?name=tujiaw&pass=123456';
var a = url.parse(href);
console.log(a);
//Url {
// protocol: 'http:',
// slashes: true,
// auth: 'user:pass',
// host: 'host.com:8080',
// port: '8080',
// hostname: 'host.com',
// hash: '#hash',
// search: '?query=string',
// query: 'query=string',
// pathname: '/p/a/t/h',
// path: '/p/a/t/h?query=string',
// href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash' }
var b = url.parse(href, true).query;
console.log(b); // { name: 'tujiaw', pass: '123456' }
var x = qs.decode('name=tujiaw&pass=123456');
console.log(x); // { name: 'tujiaw', pass: '123456' }
var y = qs.encode({name: 'hello', pass: 'world'});
console.log(y); // name=hello&pass=world
url解析
最新推荐文章于 2024-08-25 22:22:30 发布