Nodejs URL Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.