1、URL
涉及到的方法
parse():解析地址
format():生成地址
resolve(from,to):组合成地址
举例说明:
url.parse('http://baidu.com:8010/list?from=lk&lk=ooo#dd') 输出结果: { protocol: 'http:', slashes: true, auth: null, host: 'baidu.com:8010', port: '8010', hostname: 'baidu.com', hash: '#dd', search: '?from=lk&lk=ooo', query: 'from=lk&lk=ooo', //解析成字符串 pathname: '/list', path: '/list?from=lk&lk=ooo', href: 'http://baidu.com:8010/list?from=lk&lk=ooo#dd' } url.parse('http://baidu.com:8010/list?from=lk&lk=ooo#dd',true) 输出结果: { protocol: 'http:', slashes: true, auth: null, host: 'baidu.com:8010', port: '8010', hostname: 'baidu.com', hash: '#dd', search: '?from=lk&lk=ooo', query: { from: 'lk', lk: 'ooo' }, //解析成对象 pathname: '/list', path: '/list?from=lk&lk=ooo', href: 'http://baidu.com:8010/list?from=lk&lk=ooo#dd' }
url.resolve('http://baidu.com','/list') 输出结果: 'http://baidu.com/list'
2、querystring
解析query
1)序列化函数stringify()
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' }) // returns 'foo=bar&baz=qux&baz=quux&corge=' querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':') // returns 'foo:bar;baz:qux'
2)反序列化函数parse()
querystring.parse('foo=bar&baz=qux&baz=quux&corge') // returns { foo: 'bar', baz: ['qux', 'quux'], corge: '' } querystring.parse('foo:bar;baz:qux;baz:quux;corge:',';',':') { foo: 'bar', baz: [ 'qux', 'quux' ], corge: '' }
3)转义和反转义escape()和unescape()
querystring.escape('哈哈') 输出结果: '%E5%93%88%E5%93%88' querystring.unescape('%E5%93%88%E5%93%88') 输出结果: '哈哈'