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

URL of nodeJS

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

In the part of HTTP, the related knowledge of URL is introduced in detail. The url module in nodejs provides some practical functions for URL processing and parsing. This article will introduce URL in nodeJS in detail.

URL object

Parsing URL objects has the following contents, depending on whether they exist in the URL string. Any part that is not in the URL string will not appear in the parsed object

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

┌────┐│ href │├─ ─┬┬──┬─┤│ protocol ││ auth │ host │ path │ hash ││ ││ ├─┬─┼─┬─┤ ││ ││ │ hostname │ port │ pathname │ search │ ├─ ┬─┤ ││ ││ │ query │ │ "http: / / user:pass @ host.com: 8080 / p/a/t/h? Query=string # hash "│ ││ │└─┴┴─┘

[href]: complete URL ready to be parsed, including protocol and host (lowercase)

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

[protocol]: request protocol, lowercase

'http:'

[slashes]: the slash required by the protocol (after the colon)

True or false

[host]: complete lowercase host part of URL, containing port information

'host.com:8080'

[auth]: verification information in url

'user:pass'

[hostname]: lowercase hostname in the domain name

'host.com'

[port]: Port number of the host

'8080'

[pathname]: the path section in URL that contains the first slash after the hostname and before the query character

'/ pswap _ a _

[search]: query string in URL, containing the question mark at the beginning

'? query=string'

[path]: pathname and search are connected

'/ pswap _ a _

[query]: the parameter part of the query string, or the object returned after parsing with querystring.parse ()

'query=string' or {'query':'string'}

[hash]: after the "#" of the URL (including the # symbol)

'# hash'URL method

The URL module contains tools for analyzing and parsing URL. Call require ('url') to access the module

Var url = require ('url'); / * {parse: [Function: urlParse], resolve: [Function: urlResolve], resolveObject: [Function: urlResolveObject], format: [Function: urlFormat], Url: [Function: Url]} * / console.log (url)

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

Enter a URL string and return an object

The second parameter parseQueryString (default is false). If false, urlObject.query is an unparsed string, such as author=%E5%B0%8F%E7%81%AB%E6%9F%B4, and the corresponding value will not decode;. If parseQueryString is true, urlObject.query is object, such as {author: 'small match'}, and the value will be decode

The third parameter, slashesDenoteHos (default is false), if true, can correctly parse URL without protocol header. Foo in similar / / foo/bar will be considered as hostname;. If false, foo is considered to be part of pathname.

Var url = require ('url'); var str =' http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash'; / * Url {protocol: 'http:', slashes: true, auth:' user:pass', host: 'host.com:8080', port:' 80808, hostname: 'host.com', hash:' # hash', search:'? author=%E5%B0%8F%E7%81%AB%E6%9F%B4', query: 'author=%E5%B0%8F%E7%81%AB%E6%9F%B4' Pathname:'/ pathname:'/ path:'/ pAccord a Universe hacks authorizations% E5% B0% 8F% E7% 81% AB% E6% 9F% B4posts, href: 'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash'} * / console.log (url.parse (str))

Var url = require ('url'); var str =' http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash'; / * Url {protocol: 'http:', slashes: true, auth:' user:pass', host: 'host.com:8080', port:' 80808', hostname: 'host.com', hash:' # hash', search:'? author=%E5%B0%8F%E7%81%AB%E6%9F%B4', query: {author: 'Little match'}, pathname:'/ pamp Path:'/ path:'/ pAccord tAccord html authorizations% E5% B0% 8F% E7% 81% AB% E6% 9F% B4posts, href: 'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash'} * / console.log (url.parse (str,true)

Var url = require ('url'); var str =' / / foo/bar';var result1 = url.parse (str,true); var result2 = url.parse (str,true,true); console.log (result1.path); / /'/ foo/bar'console.log (result1.pathname); / /'/ / foo/bar'console.log (result1.hostname); / / nullconsole.log (result2.path); / /'/ bar'console.log (result2.pathname) / /'/ bar'console.log (result2.hostname); / / 'foo'

[url.format (urlObject)]

Reverse operation of url.parse (str), enter a parsed URL object and return the formatted string

UrlObject contains many fields, such as protocol, slashes, protocol, etc., and not all of them need to be passed, so there is a set of parsing logic.

The formatted workflow is as follows

Href will be ignored protocol regardless of whether there is a colon or not, the same will be done with http, https, ftp, gopher, file protocols will be suffixed with: / / mailto, xmpp, aim, sftp, foo, etc. Protocols: slashes if the protocol requires: / /, set to true only for the previously listed protocols without slashes For example, if mongodb://localhost:8000/auth appears, it will use .hostname only if host is missing. Port will only use host to replace hostname and portpathname if host is missing, regardless of whether the ending has / will do the same. Search will replace the query attribute regardless of whether there is / will be the same query (object). See querystring) if there is no search, hash will be used, regardless of whether it is preceded by # or not

Var url = require ('url'); var obj = {protocol:' http:', auth: 'user:pass', host:' host.com:8080', hash:'# hash', query: {author: 'matches'}} / / http://user:pass@host.com:8080?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hashconsole.log(url.format(obj));

[url.resolve (from, to)]

The url.resolve () method parses a target URL relative to a base URL in a way that the browser parses the hyperlink, with the following parameters

The relative basic URL when from is parsed. The hyperlink URL to be resolved by to. Var url = require ('url'); console.log (url.resolve (' / one/two/three', 'four')); / /' / one/two/four'console.log (url.resolve ('http://example.com/',' / one')); / / 'http://example.com/one'console.log(url.resolve('http://example.com/one',' / two')) / / 'http://example.com/two'

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

Network Security

Wechat

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

12
Report