Home >Web Front-end >Front-end Q&A >What is url in node.js

What is url in node.js

WBOY
WBOYOriginal
2022-08-19 11:34:482095browse

The url in "node.js" is the uniform resource locator, which is the abbreviation of "Uniform Resource Locator". It is an addressing method specially set up to identify the location of resources on the Internet; the url is determined by the resource type. , the host domain name where the resource is stored and the resource file name are composed of three parts. You can use the "node.js" url library to view the content of the url.

What is url in node.js

The operating environment of this article: Windows 10 system, nodejs version 16, Dell G3 computer.

What is url in node.js

Uniform Resource Locator, also called URL (Uniform Resource Locator), is specially set to identify the location of resources on the Internet. An addressing method. The web page address we usually refer to refers to the URL.

On the WWW, each information resource has a unified and unique address on the Internet. This address is called URL (Uniform Resource Locator, Uniform Resource Locator), which is the Uniform Resource Locator of the WWW. It refers to the network address.

URL consists of three parts: resource type, host domain name where the resource is stored, and resource file name.

can also be considered to be composed of 4 parts: protocol, host, port, path

The general syntax format of URL is:

(with square brackets [] is optional ):

protocol :// hostname[:port] / path / [;parameters][?query]#fragment

Use the node js url library to view the content of the URL

node js url library

//使用url库将字符串转化为对象
const url = require('url')
let urlString = 'https://127.0.0.1/wl/test/123/haha?name=123&age=18'
let urlObj = url.parse(urlString)
console.log(urlObj)

Output Result

What is url in node.js

Convert object to string

//将对象转为字符串
 
let obj = {
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: '127.0.0.1',
    port: null,
    hostname: '127.0.0.1',
    hash: null,
    search: '?name=123&age=18',
    query: 'name=123&age=18',
    pathname: '/wl/test/123/haha',
    path: '/wl/test/123/haha?name=123&age=18',
    href: 'https://127.0.0.1/wl/test/123/haha?name=123&age=18'
}
 
let string = url.format(obj)
console.log(string)

Output result

What is url in node.js

can be compared to json memory

url.parse is to convert the url string into an object

url.format is to convert the url object into a string

Recommended learning: "nodejs video tutorial

The above is the detailed content of What is url in node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn