📅  最后修改于: 2023-12-03 14:44:40.721000             🧑  作者: Mango
The Node.js urlObject.protocol
API is used to return the protocol of a URL object. The protocol is the first part of a URL that specifies the communication protocol being used by the browser to retrieve the resource. For example, https
is the protocol used to communicate with websites using HTTPS (HTTP Secure).
const url = require('url');
const urlObject = url.parse('https://www.example.com/path/foo/bar?query=string#hash_fragment');
console.log(urlObject.protocol);
This will log 'https:'
as the protocol of the given URL.
The protocol
property of the URL object is a string that specifies the protocol used by the URL. It includes the colon (:
) at the end.
// Parsing URL with different protocols
const urlObject1 = url.parse('https://www.example.com/path/foo/bar?query=string#hash_fragment');
console.log(urlObject1.protocol); // https:
const urlObject2 = url.parse('ftp://ftp.example.com/files/documents/report.pdf');
console.log(urlObject2.protocol); // ftp:
The urlObject.protocol
API is a useful method to retrieve the protocol used in a URL. It is used extensively in web development for accessing and communicating with various resources over different protocols.