Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use url Module in Node.js

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use the url module in Node.js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Url module

The URL module mainly provides properties and methods for the related operations of URL (Uniform Resource Locator, uniform Resource Locator).

A URL string is a structured string that contains multiple meaningful components. When parsed, the URL object containing the properties of each component is returned.

The url module provides two types of API for dealing with URLs: an old version of API specific to Node.js, and a new version of API that implements the same WHATWG URL standard used by Web browsers.

A comparison between WHATWG and older API is provided below.

Use WHATWG API to parse the URL string:

Const myURL = new URL ('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');

Parse the URL string using the old version of API:

Import url from 'url'; const myURL = url.parse (' https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');1. The old analytical method

The old parsing method can simply use the module's parse function, as shown below:

Var url = require ('url'); var u = "https://www.cnblogs.com/hsiang/p/15182972.html"; / / the old parsing method var obj = url.parse (u); console.log (" parsed object: "); console.log (obj)

Parse the sample screenshot, as shown below:

Note: the obj returned is an object, via obj. Property name, you can get more detailed content

two。 New parsing method / / New parsing method [ES6 Writing] const {URL} = require ('url'); var u = "https://www.cnblogs.com/hsiang/p/15182972.html"; const obj=new URL (u); console.log (" New parsing method "); console.log (obj)

Example screenshot, as shown below:

ES5 is written as follows:

/ / New parsing method [ES5 Writing] var url = require ('url'); var u = "https://www.cnblogs.com/hsiang/p/15182972.html"; var obj = new url.URL (u); console.log (" New parsing method "); console.log (obj)

Note: through the comparison, it is found that the results of the new and old methods are basically the same, with only slight differences. Please refer to the above screenshot of the new and old comparison.

Thank you for reading! This is the end of the article on "how to use the url module in Node.js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report