Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Node.js 入门系列] 统一资源定位符 url 模块 #27

Open
webfansplz opened this issue Dec 20, 2019 · 0 comments
Open

[Node.js 入门系列] 统一资源定位符 url 模块 #27

webfansplz opened this issue Dec 20, 2019 · 0 comments
Labels

Comments

@webfansplz
Copy link
Owner

webfansplz commented Dec 20, 2019

统一资源定位符 url 模块

Node.js 提供了 url 模块,用于处理与解析 URL。

1. URL 对象都有哪些属性 ?

const { URL } = require("url");

const myURL = new URL("https://github.com/webfansplz#hello");
console.log(myURL);
{
  href: 'https://github.com/webfansplz#hello',  // 序列化的 URL
  origin: 'https://github.com', // 序列化的 URL 的 origin
  protocol: 'https:', // URL 的协议
  username: '', // URL 的用户名
  password: '', //  URL 的密码
  host: 'github.com', // URL 的主机
  hostname: 'github.com',   // URL 的主机名
  port: '',  // URL 的端口
  pathname: '/webfansplz',  // URL 的路径
  search: '', // URL 的序列化查询参数
  searchParams: URLSearchParams {}, //  URL 查询参数的 URLSearchParams 对象
  hash: '#hello'  // URL 的片段
}

URL 对象属性 除了 origin 和 searchParams 是只读的,其他都是可写的.

2. 序列化 URL

const { URL } = require("url");

const myURL = new URL("https://github.com/webfansplz#hello");

console.log(myURL.href); //  https://github.com/webfansplz#hello

console.log(myURL.toString()); // https://github.com/webfansplz#hello

console.log(myURL.toJSON()); //  https://github.com/webfansplz#hello

上一节: [Node.js 入门系列] http 模块

下一节: [Node.js 入门系列] 压缩 zlib 模块

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant