My initial study Nodejs, is currently reading "Nodejs" This book, the book is very small, but let me know how to use Nodejs to create a simple small project. For example, how to create a server, such as Http.createserver, and according to the different request path to set the route selection, module introduction, create modules, Balabala things.
I have not finished reading this book yet, read it and write it again after reading it.
Today the main record is about a simple module in Nodejs, the URL module. The module of this URL needs to be introduced before it can be used. It is not necessary to require in the command line, such as CMD or Git bash, to use the URL module. can be used directly. (I don't know why not require)
The Const keyword is a constant that is defined in the ES6 and cannot be changed.
1 Const URL = require ("url");
The URL provides a total of three methods, namely Url.parse (); Url.format (); Url.resolve ();
1 url.parse (Urlstring,boolean,boolean)
Parse this method can parse a URL string and return an object with a URL
Parameter: URLString refers to a string that passes in a URL address
The second parameter (which can be saved) is passed to a Boolean value, and the default is False, which is true when the URL object is returned, and the property of query is an object.
The third parameter (which can be saved) is passed in a Boolean value, the default is False, when true, the amount, I do not know what is different, you can go to see the API.
Example 1,url.parse a case where only one parameter is passed.
1Url.parse ("Http://user:[email Protected]:8080/p/a/t/h?query=string#hash");2 /*3 return Value:4 {5 protocol: ' http: ',6 Slashes:true,7 auth: ' User:pass ',8 host: ' host.com:8080 ',9 port: ' 8080 ',Ten hostname: ' host.com ', One hash: ' #hash ', A search: '? query=string ', - query: ' query=string ', - pathname: '/p/a/t/h ', the path: '/p/a/t/h?query=string ', - href: ' http://user:[email protected]:8080/p/a/t/h?query=string#hash ' - } - when the second argument is not set to true, the query property is a string type + */
Example 2,url.parse case where the second argument is true
1Url.parse ("Http://user:[email Protected]:8080/p/a/t/h?query=string#hash",true);2 /*3 return Value:4 {5 protocol: ' http: ',6 Slashes:true,7 auth: ' User:pass ',8 host: ' host.com:8080 ',9 port: ' 8080 ',Ten hostname: ' host.com ', One hash: ' #hash ', A search: '? query=string ', - query: {query: ' string '}, - pathname: '/p/a/t/h ', the path: '/p/a/t/h?query=string ', - href: ' http://user:[email protected]:8080/p/a/t/h?query=string#hash ' - } - returns the URL object, the query property is an object + */
2 Url.format (urlobj)
Format this method is to program the incoming URL object to a URL string and return
Parameter: Urlobj refers to a URL object
Example 3,url.format
Url.format ({ protocol:"http:", Host:"182.163.0:60", Port:"$"}); /* return value: ' Http://182.163.0:60 ' */
3 url.resolve (from,to)
Resolve this method returns a string formatted as "From/to", which, in the baby's opinion, is spliced with the "/" symbol for the incoming two parameters and returns
Example 4,url.resolve
Url.resolve ("http://whitemu.com", "Gulu"); /* return value: ' Http://whitemu.com/gulu ' */
Conclusion:
OK, for the URL of this module summary is so many, if there is any understanding of the wrong place still need to correct, thank you.
Nodejs URL Module