Узел | API URL.host

Опубликовано: 6 Августа, 2021

Url.host - это встроенный интерфейс прикладного программирования класса URL с модулем in url, который используется для получения и установки хостовой части URL.

Синтаксис:

 const url.host

Возвращаемое значение: получает и устанавливает хост-часть URL-адреса.

Ниже программы иллюстрируют использование метода url.host :

Пример 1:




// node program to demonstrate the 
// url.host API as Setter 
   
//importing the module "url"
const http = require("url");
   
// creating and initializing myURL
const myURL = new URL("https://example.com:80/foo#ram");
   
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
   
// assinging host portion
// using host
console.log();
myURL.host = "example.com:82";
   
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);

Выход :

Перед изменением
https://example.com:80/foo#ram

После изменения
https://example.com:82/foo#ram

Пример 2:




// node program to demonstrate the
// url.host API as Getter
//importing the module 'url'
const http = require( 'url' );
// creating and initializing myURL
const myURL = new URL( ' https://example.org:82/foo#ram ' );
// getting the host portion
// using host
const host = myURL.host;
// Display hash value
console.log(host);

Выход:

 example.org:82

ПРИМЕЧАНИЕ . Приведенная выше программа будет скомпилирована и запущена с помощью команды node myapp.js .

Справка:
https://nodejs.org/dist/latest-v10.x/docs/api/url.html#url_url_host