The URL module provides some useful functions for URL processing and parsing.
Const URL = require (' URL ')//Introduction URL module
1.url string with URL object
A URL string is a structured string that contains a number of meaningful components. When parsed, returns a URL object that contains each component as a property.
url
The module provides two sets of APIs to handle URLs: One is a unique API left by node. JS, and the other is the API that typically implements the WHATWG URL standard in a Web browser.
Note that although the unique API that is left behind by node. JS is not deprecated, it is reserved for backwards compatibility with existing applications. Therefore, the new application uses the WHATWG API.
Parse a URL string using the WHATWG API:
const { URL } = require(‘url‘); const myURL =
new URL(‘https://user:[email protected]:8080/p/a/t/h?query=string#hash‘);
In the browser, WHATWG is URL
always available globally, and in node. js, in any case, opening or using a link must refer to the ' URL ' module in advance:require(‘url‘).URL
Resolve a URL by using the API provided by node. JS:
const url = require(‘url‘); const myURL = url.parse(‘https://user:[email protected]:8080/p/a/t/h?query=string#hash‘);
The 2 URL provides some methods
Url.parse (urlstring)//String type resolved to Object Url.format (Urlobject)//object type to String Url.resolve (from,to)//Path Stitching tips: Node environment Execute and view the URL Object command: Node enter >url.parse ("http://www.baidu.com") as shown using Url.parse () parsing www.baidu.com
3.URLSearchParams and URL methods: URLs are the objects used to create the URL object, Rulsearchparams this thing is specifically used to query the URL to pass the data. But the relationship between them has some small complex const {URL, urlsearchparams} = require ("url");A brain teaser: let Myurl = new URL ("https://www.baidu.com?abc=123"), Urlsearchparams = new Urlsearchparams (myurl.search); Urlsearchparams.set ("ABB", "456"); Console.log (urlsearchparams); Console.log (myurl.href);
//What is the result here? Does our operation on urlsearchparams affect Myurl? 4.parse: Some special address: More than tablets, CDN acceleration of things what, like to use this way to address the delivery://static.lagou.com/i/image/m00/01/80/ Cgqkkvzupraan3o6aacfebgn7dy620.jpg This type of address has a very graphic name called:protocol-free address。 So when we parse the protocol-free address: it will be parsed into this bear-like: this is really a wooden agreement! You can paste this code directly into the browser, the browser will return to you a file://protocol, into a local disk search. URL {protocol:null, slashes:null, Auth:null, Host:null, Port:null, Hostname:null, Hash:null, Search:null, Query:null, pathname: '//static.lagou.com/i/image/m00/01/80/cgqkkvzupraan3o6aacfebgn7dy620.jpg ', Path: '// Static.lagou.com/i/image/m00/01/80/cgqkkvzupraan3o6aacfebgn7dy620.jpg ', href: '//static.lagou.com/i/image/m00/01 /80/cgqkkvzupraan3o6aacfebgn7dy620.jpg '} encountered such an address really can't parse it? Of course not, look at the file below, note the third parameter of the document. So just change: Url.parse () The third argument to true, it's all right. Format inverse operation, obj = = string. Resolve a collection of domain names and paths: View this result to see the function: Url.resolve ("http://www.baidu.com", "/b") See this result to see the true effect: Url.resolve ("http// www.baidu.com/a ","/b ")
Nodejs URL Module