node.js 基础api

cmd  进入命令

node  进入node 环境

1、  url  

输入  url  会显示出  url 对象包含的方法

{ parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  Url: [Function: Url] }

    1.1  parse  解析地址

    url.parse(' http://write.blog.csdn.net/postedit?a=1&b=2#/home ')

   解析出:

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'write.blog.csdn.net',
  port: null,  //端口
  hostname: 'write.blog.csdn.net',
  hash: '#/home',
  search: '?a=1&b=2', 
  query: 'a=1&b=2',
  pathname: '/postedit',   
  path: '/postedit?a=1&b=2',
  href: 'http://write.blog.csdn.net/postedit?a=1&b=2#/home' }

第二个参数

url.parse(' http://write.blog.csdn.net/postedit?a=1&b=2#/home ' , true)   有第二参数,默认是 false  , 加上 true 后里面的  query 参数会变成一个对象

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'write.blog.csdn.net',
  port: null,
  hostname: 'write.blog.csdn.net',
  hash: '#/home',
  search: '?a=1&b=2',
  query: { a: '1', b: '2' },
  pathname: '/postedit',
  path: '/postedit?a=1&b=2',
  href: 'http://write.blog.csdn.net/postedit?a=1&b=2#/home' }
第三个参数

     url.parse(' //write.blog.csdn.net/postedit?a=1&b=2#/home ' , true ,  true)  //默认是false   , 在不知道  是什么开头的情况下(http  或者https)加上第三个参数可正常解析

Url {
  protocol: null,
  slashes: true,
  auth: null,
  host: 'write.blog.csdn.net',
  port: null,
  hostname: 'write.blog.csdn.net',
  hash: '#/home',
  search: '?a=1&b=2',
  query: { a: '1', b: '2' },
  pathname: '/postedit',
  path: '/postedit?a=1&b=2',
  href: '//write.blog.csdn.net/postedit?a=1&b=2#/home' }



    1.2 format  反解析

    url.format(  { …以上解析出来的 Url 对象 …  } );

会得出   http://write.blog.csdn.net/postedit?a=1&b=2#/home;
  

   1.3  resove  

url.resove( 'http://xxx.com/ ', ' /home/1' );

合成一个路径  http://xxx.com//home/1



2、  querystring   对 url 后面的进行操作

2.1querystring.stringify()    把对象数据转换成  url上传参型的数据

在命令里  输入
querystring.stringify({name:'aa',course:['1','2','3'],from:''})
会转换成
'name=aa&course=1&course=2&course=3&from='

第二个参数是  那个 & 符用什么来代替   
第二个参数是 那个 = 号用什么来代替

2.2、  querystring.parse()   把上面转换的解析成原来

querystring.parse('name=aa&course=1&course=2&course=3&from=')


会解析成

{name:'aa',course:['1','2','3'],from:''}

第二个参数是  如果有代替了 & 符的就写上 代替的  
第二个参数是  如果有代替了 & 符的就写上 代替的  

2.3、  querystring.escape('<呵呵>')  , 对url 转义

'%3C%E5%91%B5%E5%91%B5%3E'

2.3     querystring.unescape('%3C%E5%91%B5%E5%91%B5%3E')   转回来

<呵呵>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值