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 nodejs

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

Share

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

This article mainly introduces the relevant knowledge of how to use the url module in nodejs, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the url module in nodejs. Let's take a look at it.

In nodejs, the URL module is used to process and parse URL, which provides three methods, namely: 1, the parse method is used to parse the url into an object, 2, the format method is used to convert the object into a url string, and 3, the resolve method is used to splice the URL.

This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.

The usage of url Module in nodejs

The URL module is used to parse and process URL strings and provides three methods:

1. Parse method

Parse the url into an object, and the parse method prototype:

Url.parse (urlStr [, parseQueryString] [, slashesDenoteHost])

Three parameters can be passed, the first of which must be

UrlStr: the url string to parse into an object

ParseQueryString: whether to parse query parameters. Default is false.

SlashesDenoteHost: whether to resolve hostnames with slashes. Default is false.

Only the first parameter is given:

Var url = require ('url'); var testUrl1 =' z.qingk.cn/information-info/tengzhou/e75bab8405254b04b2fab0a9f54e4838/dbvbotovboussowbwssvossdqaxqssxw/7519fbc4d85ea4c3589d8d751c507222/b011a8c5bbab4050953cb722bccb5095?param1=111¶m2=222'var urlParseUrl1 = url.parse (testUrl1) console.log (urlParseUrl1)

Results:

Second parameter is set to true

In other words, you have to put it in the url at the same time? The subsequent query parameters are parsed into objects

Var url = require ('url'); var testUrl1 =' z.qingk.cn/information-info/tengzhou/e75bab8405254b04b2fab0a9f54e4838/dbvbotovboussowbwssvossdqaxqssxw/7519fbc4d85ea4c3589d8d751c507222/b011a8c5bbab4050953cb722bccb5095?param1=111¶m2=222'var urlParseUrl1 = url.parse (testUrl1); console.log (urlParseUrl1); var urlParseUrl2 = url.parse (testUrl1,true); console.info (urlParseUrl2)

Results:

The third parameter is set to true

That is, when the url protocol is not known, the host is identified on the basis of / /.

Var testUrl2 ='/ / z.qingk.cnqingk.cncontrol InformationFoward tengzhouache e75bab8405254b04b2fab0a9f54e4838dbvbotoboussowbwssvossdqaxqssxw 7519fbc4d85ea4c3589d8d751c507222market b011a8c5bbab4050953cb722bcb5095roomparam1111param2222roomparam1111param2222portable urlParseUrl3 = url.parse (testUrl2,false,false); console.info (urlParseUrl3); var urlParseUrl4 = url.parse (testUrl2,false,true); console.info (urlParseUrl4)

Results:

2. Format method

Format is the return process of parse, converting objects into url strings.

Var testObj1 = {protocol: null, slashes: true, auth: null, host: 'z.qingk.cnails, port: null, hostname:' z.qingk.cnails, hash: null, search:'? param1=111¶m2=222' Query: 'param1=111¶m2=222', pathname:' / information-info/tengzhou/e75bab8405254b04b2fab0a9f54e4838/dbvbotovboussowbwssvossdqaxqssxw/7519fbc4d85ea4c3589d8d751c507222/b011a8c5bbab4050953cb722bccb5095', path:'/ information-info/tengzhou/e75bab8405254b04b2fab0a9f54e4838/dbvbotovboussowbwssvossdqaxqssxw/7519fbc4d85ea4c3589d8d751c507222/b011a8c5bbab4050953cb722bccb5095?param1=111¶m2=222', href:'/ z.qingk.cnscarp InformationChart tengzhouabe75bab8405254b04b2fab0a9f54e4838dbvbotovboussowbwssvossdqaxqssxwand7519fb4d85ea4c3589d8d751c507222max b011a8c5bbab4050953cb722bccb5095param119fbc4d85ea4c3589d8d751c507222 Var rsUrl = url.format (testObj1); console.info (rsUrl)

Results:

/ / z.qingk.cn/information-info/tengzhou/e75bab8405254b04b2fab0a9f54e4838/dbvbotovboussowbwssvossdqaxqssxw/7519fbc4d85ea4c3589d8d751c507222/b011a8c5bbab4050953cb722bccb5095?param1=111¶m2=222

Format can transform the results generated by each form of url in parse into the inverse process.

3. Resolve method

Returns the absolute path url specified from the root directory to the current directory. Returns the result without parameters and anchor points, and returns the standard url path format of the result.

Var url=require ('url'); / / specify the relative path var url1=url.resolve (' http://qingk.cn/one/two/three','four'); console.log (url1); / / http://qingk.cn/one/two/four / / specify the relative path of the root directory var url3=url.resolve ('http://qingk.cn/one/two/three','/four'); console.log (url3)) / / http://qingk.cn/four / / relative path var url2=url.resolve with parameters ('http://qingk.cn/one/two/three?name=zhangsan','four'); console.log (url2); / / http://qingk.cn/one/two/four / / original path with non-standard delimiter var url4=url.resolve (' http://qingk.cn\\one#name1','/four');) Console.log (url4); / / http://qingk.cn/four / / the relative path of the non-standard delimiter var url5=url.resolve ('http://qingk.cn/one','\\two\\three'); console.log (url5); / / http://qingk.cn/two/three 's article on "how to use the url module in nodejs" ends here, thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the url module in nodejs". If you want to learn more, you are welcome to follow the industry information channel.

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