Node.js学习笔记(四)#Url网址模块

目录

一、url模块

二、url模块:new URL()

1.简介

2.用法

3.URL对象

4.常用API

1)url.href

2)url.pathname

3)url.search

三、url模块:new URLSearchParams()

1.简介

qs查询字符串模块https://blog.csdn.net/weixin_42214717/article/details/128080160

2.用法

1)创建空URLSearchParams对象

2)三种传参方式

3.常用API

1)urlSearchParams.append(name, value)

2)urlSearchParams.get(name)

3)urlSearchParams.set(name, value)

4)urlSearchParams.delete(name)


一、url模块

URL字符串是包含多个有意义的部分的结构化字符串。解析后,将返回一个URL对象,其中包含每个部分的属性。

如今,我们使用WHATWG URL标准,WHATWG URLorigin属性包括protocolhost,但不包括usernamepassword

二、url模块:new URL()

1.简介

url模块是node.js中用来处理url的模块

由于node.js的url.formatparse等方法采用的传统的urlObject,不符合URL现存标准-WHATWG API。所以我们使用new URL()来代替url模块

2.用法

new URL(input[, base])

input <string> 必填参数 可以解析绝对或相对路径,若input为相对路径,则需要加上base路径,若input为绝对路径,则忽略base路径

base <string>|<URL> 可缺省参数 相对于base路径来解析input路径

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');

3.URL对象

new URL('https://localhost:3000/person?id=1&weight=60') 会解析地址并返回一个URL对象,返回对象如下。其中的key除了originsearchParams是只读key,其余的key皆可以进行读写操作。

URL {
  href: 'https://localhost:3000/person?id=1&weight=60',
  origin: 'https://localhost:3000',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'localhost:3000',
  hostname: 'localhost',
  port: '3000',
  pathname: '/person',
  search: '?id=1&weight=60',
  searchParams: URLSearchParams { 'id' => '1', 'weight' => '60' },
  hash: ''
}

4.常用API

1)url.href

①作用:获取或写入整个url地址

②读取:

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');
console.log(myURL.href);
// 输出 https://localhost:3000/person?id=1&weight=60

③写入:

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');
myURL.href = 'http://127.0.0.1:4000/zhangsan?id=2&weight=80'
console.log(myURL.href)
// 输出 http://127.0.0.1:4000/zhangsan?id=2&weight=80

2)url.pathname

①作用:获取或写入地址的路径名

②读取:

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');
console.log(myURL.pathname); 
// 输出 /person

③写入:

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');
myURL.pathname = 'zhangsan'
console.log(myURL.href);
// 输出 https://localhost:3000/zhangsan?id=1&weight=60

3)url.search

①作用:获取或写入请求参数部分

②读取:

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');
console.log(myURL.search)
// 输出 ?id=1&weight=60

③写入:

const myURL = new URL('https://localhost:3000/person?id=1&weight=60');
myURL.search = 'weight=90'
console.log(myURL.search)
// 输出 ?weight=90

三、url模块:new URLSearchParams()

1.简介

new URLSearchParams()会实例化新的空URLSearchParams对象。用来解析查询字符串。但是组装参数的操作比较繁琐,所以后续我们将用外置的qs模块代替,以达到更优雅地操作请求参数。

qs查询字符串模块https://blog.csdn.net/weixin_42214717/article/details/128080160

2.用法

1)创建空URLSearchParams对象

const params = new URLSearchParams()

2)三种传参方式

//1.传入字符串
const params = new URLSearchParams('user=zhangsan&height=180')
console.log(params.get('user')) // 输出 zhangsan
//2.传入对象
const params = new URLSearchParams({
    user: 'zhangsan',
    feature: ['weight', 'height']
})
console.log(params.toString()); // 输出 user=zhangsan&feature=weight%2Cheight
//3.传入iterable类型
const params = new URLSearchParams([
  ["user", "zhangsan"],
  ["height", "180"],
  ["weight", "70"],
]);
console.log(params.toString()); // 输出 user=zhangsan&height=180&weight=70

3.常用API

1)urlSearchParams.append(name, value)

①作用:

将新的名称值对附加到查询字符串

②用法:

const params = new URLSearchParams('user=zhangsan&height=180')
params.append('weight','70')
console.log(params.toString());
// 输出 user=zhangsan&height=180&weight=70

2)urlSearchParams.get(name)

①作用:

返回key为name的第一个key的值

②用法:

const params = new URLSearchParams('user=zhangsan&height=180')
console.log(params.get('height'));
// 输出 180

3)urlSearchParams.set(name, value)

①作用:

设置URLSearchParams对象中与name关联的值.

若存在多个name,则会设置第一个并移除其他的name。若不存在,则会进行append操作

②用法:

const params = new URLSearchParams('user=zhangsan&height=180&height=170')
params.set('height','190')
params.set('weight','80')
console.log(params.toString());
// 输出 user=zhangsan&height=190&weight=80

4)urlSearchParams.delete(name)

①作用:

删除所有键为name的键值对

②用法:

const params = new URLSearchParams('user=zhangsan&height=180&weight=70')
params.delete('weight')
console.log(params.toString());
// 输出 user=zhangsan&height=180

猜你喜欢

转载自blog.csdn.net/weixin_42214717/article/details/128034360