Previous words
In the HTTP section, the knowledge of the URL is described in detail. The URL module in Nodejs provides some useful functions for URL processing and parsing. This article describes the URLs in Nodejs in detail
URL Object
The parse URL object has the following content, depending on whether they exist in the URL string. Any part that is not in the URL string will not appear in the Parse object.
' Http://user:[email Protected]:8080/p/a/t/h?query=string#hash '
┌─────────────────────────────────────────────────────────────────────────────┐│ href│├──────────┬┬───────────┬─────────────────┬─ ──────────────────────────┬───────┤│protocol││auth│host│path│hash││ ││├──────────┬──────┼──────────┬────────────────┤│││││hostname│port│pathnam E│search││││││││├─┬──────────────┤││││ │││││query││ "http://User:pass @ host.com:8080/p/a/t/ H? Query=string #hash "││││││││ ││└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘
"href": Ready to parse the full URL, containing the protocol and host (lowercase)
' Http://user:[email Protected]:8080/p/a/t/h?query=string#hash '
"Protocol": request agreement, lowercase
' http: '
"Slashes": the slash required by the protocol (after the colon)
true false
"Host": Full URL Lowercase host section with port information
' host.com:8080 '
"Auth": Authentication information in the URL
' User:pass '
"hostname": the lowercase hostname in the domain name
' Host.com '
"Port": Port number of the host
' 8080 '
"Pathname": The path part of the URL, after the host name, before the query character, contains the first slash
'/p/a/t/h '
"Search": query string in URL, containing the question mark at the beginning
'? query=string '
"Path": pathname and search are linked together
'/p/a/t/h?query=string '
"Query": the parameter portion of the query string, or the object returned after parsing with Querystring.parse ()
' query=string ' or {' query ': ' String '}
"Hash": The following part of the "#" of the URL (including the # symbol)
' #hash '
Url method
The URL module contains tools for parsing and parsing URLs. Call require (' URL ') to access the module
var url = require (' URL '); /* {parse: [function:urlparse], resolve: [Function:urlresolve], resolveobject: [Function: Urlresolveobject], format: [Function:urlformat], */console.log (URL);
"Url.parse (urlstr[, parsequerystring][, Slashesdenotehost])"
Enter a URL string to return an object
The second parameter is parsequerystring (false by default), and if False, Urlobject.query is an unresolved string, such as AUTHOR=%E5%B0%8F%E7%81%AB%E6%9F%B4, The corresponding value is not decode; If Parsequerystring is true, then Urlobject.query is an object, such as {author: ' Little Match '}, and the value is decode
The third parameter is Slashesdenotehos (false by default), and if true, the URL without the protocol header can be parsed correctly, and Foo like//foo/bar is considered hostname; Then Foo is considered part of the pathname.
var URL = require (' URL ' var str = ' http://user:[email protected]: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: ' 8080 ', 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: '/p/a/t/h ', Path: '/p/a/t/h?author=%e5%b0%8f%e7%81%ab%e6%9f%b4 ', href: ' http://user:[ Email protected]: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:[email protected]: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: ' 8080 ', hostname: ' host.com ', hash: ' #hash ', search: '? author=%e5%b0%8f%e7%81%ab%e6%9f%b4 ', Query: {Author: ' Little Match '}, pathname: '/p/a/t/h ', path: '/p/a/t/h?author=%e5%b0%8f%e7%81%ab%e6%9f%b4 ', HREF: ' Http://user:[email protected]:8080/p/a/t/h?author=%e5%b0%8f%e7%81%ab%e6%9f%b4#hash '} */ Console.log (Url.parse (str,true));
varurl = require (' URL '));varstr = '//foo/bar ';varRESULT1 = Url.parse (str,true);varRESULT2 = 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)"
Url.parse (str) Reverse operation, enter a parsed URL object, return the formatted string
Urlobject contains a number of fields, such as protocol, slashes, protocol, etc., and does not necessarily need to pass all, so there is a set of analytic logic
The formatted workflow is as follows
href will be ignored protocol whether or not there is a trailing: (colon), the same processing HTTP, HTTPS, FTP, Gopher, the file protocol will be added suffix: Span>// mailto, XMPP, Aim, SFTP, Foo, and other protocols add suffixes: Slashes if the protocol requires: // Only those protocols without slashes listed previously, such as Mongodb:// localhost:8000 / auth will be used if present. hostname only use port when host is missing use host to replace hostname and Portpath only when host is missing Name regardless of whether the end has / will also handle search will override the query property regardless of whether there is a /
var url = require (' URL '); var obj = { ' http: ' , ' user:pass ', ' host.com:8080 ', ' #hash ', ' Little Match ' }}//http://user:[email protected]: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 destination URL into a base URL in a way that a browser parses a hyperlink, with the following parameters
From <String><String> The URL of the hyperlink to parse.
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 ' // ' Http://example.com/two '
URL of Nodejs