nodeJS - 7 - URL、querystring模块

URL 模块 与 querystring 模块

说明

url 模块提供了一些实用函数,用于 url处理与解析,url 字符串是一个结构化的字符串,包含多个有意义的组成部分,被解析时,会返回一个 URL 对象,包含每个组成部分的属性

querystring 模块提供了一些实用的函数,用于解析与格式化 URL 查询字符串

参考

URL 模块 API

v7.0.0 版本开始新增 WHATWG URL Standard的API,旧有的 API 没有被弃用,这里只列出 新版本的API

  1. new URL(input[,base]) 通过将input解析到base上创建一个新的URL对象
  2. href 获取\设置 url
  3. protocol 获取\设置 协议部分
  4. hostname 获取\设置 主机名
  5. port 获取\设置 端口部分
  6. host 获取\设置 主机(hostname + port)部分
  7. origin 获取 来源部分(protocol + host)
  8. pathname 获取\设置 路径部分
  9. search 获取\设置 序列化查询部分
  10. hash 获取\设置 hash 部分
  11. password 获取\设置 密码 部分
  12. username 获取\设置 用户名 部分
  13. toString() 返回序列换的 url
  14. toJSON() 返回序列化的URL
  15. searchParams 获取表示URL查询参数的URLSearchParams对象。该属性是只读的
    const {URL} = require('url');

    // 创建 URL 对象
    const myURL = new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');

    console.log('href', myURL.href);
    console.log('protocol', myURL.protocol);
    console.log('hostname', myURL.hostname);
    console.log('port', myURL.port);
    console.log('host', myURL.host);
    console.log('origin', myURL.origin);
    console.log('pathname', myURL.pathname);
    console.log('search', myURL.search);
    console.log('hash', myURL.hash);
    console.log('password', myURL.password);
    console.log('username', myURL.username);
    console.log('toString', myURL.toString());
    console.log('toJSON', myURL.toJSON());
    /*
        href https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash
        protocol https:
        hostname sub.host.com
        port 8080
        host sub.host.com:8080
        origin https://sub.host.com:8080
        pathname /p/a/t/h
        search ?query=string
        hash #hash
        password pass
        username user
        toString https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash
        toJSON https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash
    */

URL 模块 URLSearchParams

URLSearchParams 接口提供对 URL query 部分的读写权限

  1. new URLSearchParams() 实力化,可以传入一个查询字符串作为参数,或者传入特定格式的对象
  2. append(name,value) 添加新的查询到 name 上
  3. delete(name) 删除某一个查询
  4. get(name) 返回 name 对应的查询的第一个值
  5. getAll(name) 返回 name 对应查询的所有值
  6. has(name) 返回是否存在 名为 name 的查询
  7. set(name, value) 将名为 name 的查询的值设置为 value ,并删除其他值
    const {URL, URLSearchParams} = require('url');

    // 创建 URL 对象
    const myURL = new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');

    console.log(myURL.searchParams);

    // 参数可以是字符串
    const params1 = new URLSearchParams('name=bbb&age=89');

    // 参数可以是对象
    const params2 = new URLSearchParams({
      user: 'abc',
      query: ['first', 'second']
    });

    // 参数可以是 URL 的 searchParams 对象
    const params3 = new URLSearchParams(myURL.searchParams);

    // append 添加查询
    params1.append('sex', 'male');

    // delete 删除查询
    params2.delete('user');

    // get 返回查询值
    params2.get('query'); // first,second

    // getAll 返回查询所有值
    params2.getAll('query'); // ["first,second"]

    // has 是否存在查询
    params1.has('name'); // true

    // set 更改查询值,如果有多个值则变为一个
    params2.set('query', 'none');

    console.log(params1.toString()); // name=bbb&age=89&sex=male
    console.log(params2.toString()); // query=none 
    console.log(params3.toString()); // query=string

querystring 模块 API

  1. parse(str[, sep[, eq[, options]]]) str: 要解析的字符串;sep,eq 是对应一般 &= 的分隔符;options 配置
  2. stringify(obj[, sep[, eq[, options]]]) obj: 要序列化成 URL 查询字符串的对象;sep,eq 是对应一般 &= 的分隔符;options 配置
    const querystring = require('querystring');

    const protoParams = 'name=aaa&age=90&sex=male&change=true';

    const params1 = querystring.parse(protoParams);

    console.log(params1); // {name: "aaa", age: "90", sex: "male", change: "true"}

    const newStr = querystring.stringify(params1, ';', '*');

    console.log(newStr); // name*aaa;age*90;sex*male;change*true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值