url模块

2018-08-24  本文已影响0人  金桔柠檬加冰

url模块

//引入url模块
var myUrl = request('url')

url.hash

获取及设置URL的分段(hash)部分。

var myUrl = require('url');
var myURL = new myUrl('https://example.org/foo#bar');
console.log(myURL.hash);
  // 输出 #bar
//赋值
myURL.hash = 'baz';
console.log(myURL.href);
  // 输出 https://example.org/foo#baz

包含在赋给hash属性的值中的无效URL字符是百分比编码。请注意选择哪些字符进行百分比编码可能与url.parse()url.format()方法产生的不同。

url.host

获取及设置URL的主机(host)部分。

var Url = require('url');
var myURL = new Url('https://example.org:81/foo');
console.log(myURL.host);
  // 输出 example.org:81

myURL.host = 'example.com:82';
console.log(myURL.href);
  // 输出 https://example.com:82/foo

如果给host属性设置的值是无效值,那么该值将被忽略。

url.hostname

获取及设置URL的主机名(hostname)部分。 url.hosturl.hostname之间的区别是url.hostname 包含端口。

var URL = require('url');
const myURL = new URL('https://example.org:81/foo');
console.log(myURL.hostname);
  // 输出 example.org

myURL.hostname = 'example.com:82';
console.log(myURL.href);
  // 输出 https://example.com:81/foo

如果给hostname属性设置的值是无效值,那么该值将被忽略。

url.href

获取及设置序列化的URL。

var URL = require('url');
const myURL = new URL('https://example.org/foo');
console.log(myURL.href);
  // 输出 https://example.org/foo

myURL.href = 'https://example.com/bar';
console.log(myURL.href);
  // 输出 https://example.com/bar

获取href属性的值等同于调用url.toString()

将此属性的值设置为新值等同于new URL(value)使用创建新的URL对象。URL对象的每个属性都将被修改。

如果给href属性设置的值是无效URL,将会抛出TypeError

url.origin

获取只读序列化的URL origin部分。

const { URL } = require('url');
const myURL = new URL('https://example.org/foo/bar?baz');
console.log(myURL.origin);
  // 输出 https://example.org
const { URL } = require('url');
const idnURL = new URL('https://你好你好');
console.log(idnURL.origin);
  // 输出 https://xn--6qqa088eba

console.log(idnURL.hostname);
  // 输出 xn--6qqa088eba

url.password

获取及设置URL的密码(password)部分。

url.pathname

获取及设置URL的路径(path)部分。

url.port

获取及设置URL的端口(port)部分。

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

更多查询

上一篇 下一篇

猜你喜欢

热点阅读