url

 将URL字符串解析为URL 对象:

const url = require('url');

url.parse('https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1',true);

/*
{ protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?ie=utf-8&f=8&rsv_bp=1',
  query: { ie: 'utf-8', f: '8', rsv_bp: '1' },
  pathname: '/s',
  path: '/s?ie=utf-8&f=8&rsv_bp=1',
  href: 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1' }
*/

query部分的读写权限:

const url = require('url');

let myURL = new URL('https://www.baidu.com/s?ie=utf-8&f=8');

let search = myURL.searchParams;  //{ 'ie' => 'utf-8', 'f' => '8' }

console.log( search.get('ie') );   //读取'ie'键,结果:'utf-8'

search.set('ie','hex');   //设置'ie'键的值为'hex'
console.log( search.get('ie') );      //'hex'
console.log(myURL.href);    //https://www.baidu.com/s?ie=hex&f=8

search.delete('ie');        //删除'ie'键
console.log(myURL.href);    //https://www.baidu.com/s?f=8

search.append('a','a');     //增加'a'键
console.log(myURL.href);    //https://www.baidu.com/s?f=8&a=a

 

你可能感兴趣的:(node)