zoukankan      html  css  js  c++  java
  • node.js在windows下的学习笔记(10)---URL模块

    1.parse函数的作用是解析url,返回一个json格式的数组

    url.parse('http://www.zjut.edu.cn');
    { protocol: 'http:',
      slashes: true,
      auth: null,
      host: 'www.zjut.edu.cn',
      port: null,
      hostname: 'www.zjut.edu.cn',
      hash: null,
      search: null,
      query: null,
      pathname: '/',
      path: '/',
      href: 'http://www.zjut.edu.cn/' }
    > 

    2.parse函数的第二个参数是布尔类型,当参数为true时,会将查询条件也解析成json格式的对象。

    url.parse('http://www.baidu.com?page=1',true);
    { protocol: 'http:',
      slashes: true,
      auth: null,
      host: 'www.baidu.com',
      port: null,
      hostname: 'www.baidu.com',
      hash: null,
      search: '?page=1',
      query: { page: '1' },
      pathname: '/',
      path: '/?page=1',
      href: 'http://www.baidu.com/?page=1' }
    > 

    3.parse函数的第三个参数也是布尔类型的,当参数为true,解析时会将url的"//"和第一个"/"之间的部分解析为主机名

    url.parse('http://www.baidu.com/news',false,true);
    { protocol: 'http:',
      slashes: true,
      auth: null,
      host: 'www.baidu.com',
      port: null,
      hostname: 'www.baidu.com',
      hash: null,
      search: null,
      query: null,
      pathname: '/news',
      path: '/news',
      href: 'http://www.baidu.com/news' }
    > 

    4.format函数的作用与parse相反,它的参数是一个JSON对象,返回一个组装好的url地址

    url.format({
    protocol: 'http:',
    hostname:'www.baidu.com',
    port:'80',
    pathname :'/news',
    query:{page:1}
    });
    'http://www.baidu.com:80/news?page=1'

    5.resolve函数的参数是两个路径,第一个路径是开始的路径或者说当前路径,第二个则是想要去往的路径,返回值是一个组装好的url

    url.resolve('http://example.com/', '/one')
    'http://example.com/one'
  • 相关阅读:
    python相关遗漏知识点补充
    关于viewpager的滑动问题
    C++学习一二
    Neo4j 爬坑笔记for3.2.6
    ZTree简单粗暴快速使用
    阅读HashMap——jdk7时遇到的问题记录
    【安装】Hadoop2.8.0搭建过程整理版
    html、jsp页面标签的遍历
    tomcat配置多个数据源
    java线程
  • 原文地址:https://www.cnblogs.com/michaeljunlove/p/3975060.html
Copyright © 2011-2022 走看看