URL 模块 与 querystring 模块
说明
url 模块提供了一些实用函数,用于 url处理与解析,url 字符串是一个结构化的字符串,包含多个有意义的组成部分,被解析时,会返回一个 URL 对象,包含每个组成部分的属性
querystring 模块提供了一些实用的函数,用于解析与格式化 URL 查询字符串
参考
URL 模块 API
v7.0.0 版本开始新增 WHATWG URL Standard的API,旧有的 API 没有被弃用,这里只列出 新版本的API
new URL(input[,base])
通过将input解析到base上创建一个新的URL对象href
获取\设置 urlprotocol
获取\设置 协议部分hostname
获取\设置 主机名port
获取\设置 端口部分host
获取\设置 主机(hostname + port)部分origin
获取 来源部分(protocol + host)pathname
获取\设置 路径部分search
获取\设置 序列化查询部分hash
获取\设置 hash 部分password
获取\设置 密码 部分username
获取\设置 用户名 部分toString()
返回序列换的 urltoJSON()
返回序列化的URLsearchParams
获取表示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 部分的读写权限
new URLSearchParams()
实力化,可以传入一个查询字符串作为参数,或者传入特定格式的对象append(name,value)
添加新的查询到 name 上delete(name)
删除某一个查询get(name)
返回 name 对应的查询的第一个值getAll(name)
返回 name 对应查询的所有值has(name)
返回是否存在 名为 name 的查询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
parse(str[, sep[, eq[, options]]])
str: 要解析的字符串;sep,eq 是对应一般&
与=
的分隔符;options 配置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