首页 > 其他分享 >url模块

url模块

时间:2023-02-27 20:48:31浏览次数:26  
标签:21 url age 模块 null zhangsan name

url模块用于解析url

例:

const url = require('url');

var api = 'https://www.baidu.com?name=zhangsan&age=21';

// console.log(url.parse(api));
/*
Url {
  protocol: 'https:',   
  slashes: true,        
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=21',
  query: 'name=zhangsan&age=21',
  pathname: '/',
  path: '/?name=zhangsan&age=21',
  href: 'https://www.baidu.com/?name=zhangsan&age=21'
}
 */

//我们再传递一个参数true,即可把query封装成一个对象
// console.log(url.parse(api,true));
/*
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=21',
  query: [Object: null prototype] { name: 'zhangsan', age: '21' },
  pathname: '/',
  path: '/?name=zhangsan&age=21',
  href: 'https://www.baidu.com/?name=zhangsan&age=21'
}
*/
var temp = url.parse(api,true).query;
console.log(temp);//[Object: null prototype] { name: 'zhangsan', age: '21' }
console.log(temp.name);//zhangsan
console.log(temp.age);//21

 

标签:21,url,age,模块,null,zhangsan,name
From: https://www.cnblogs.com/qianduanLamp/p/16742061.html

相关文章