笔记nodejs模块:url.parse和querystring.parse
1.url.parse
将一个URL字符串转换成对象并返回。
var url = require('url');
var a = url.parse('http://localhost:8080/one?a=index&t=article');
console.log(a);
//输出结果:
{
protocol : 'http' ,
auth : null ,
host : 'localhost:8080' ,
port : '8080' ,
hostname : 'localhost' ,
hash : null ,
search : '?a=index&t=article',
query : 'a=index&t=article',
pathname : '/one',
path : '/one?a=index&t=article',
href : 'http://localhost:8080/one?a=index&t=article'
}
2.querystring.parse
将字符串转成对象。说白了其实就是把url上带的参数串转成数组对象(上述query项)
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }