node. JS study fourth day--url processing

Source: Internet
Author: User

1. Basic usage of the parse function

The parse function is to parse the URL and return an array in JSON format, see the example below:

var url = require (' URL '); Url.parse (' http://www.baidu.com ');
Operation Result:
{protocol: ' http: ',  slashes:null,  auth:null,  host:null,  port:null,  hostname:null,  hash: NULL,  search:null,  query:null,  pathname: ' www.baidu.com ',  path: ' www.baidu.com ',  href: ' http ://www.baidu.com '}

Try passing in a URL to see what the return result is.

2. Parse function--conditional parsing

The second argument to the parse function is a Boolean type, and when the argument is true, the query condition is also parsed into a JSON-formatted object.

var url = require (' URL '); Url.parse (' http://www.baidu.com?page=1 ', true);
Operation Result:
{protocol: ' http: ',  slashes:true,  auth:null,  host: ' www.baidu.com ',  port:null,  hostname: ' Www.baidu.com ',  hash:null,  search: '? page=1 ',  query: {page: ' 1 '},  pathname: '/',  path: '/?page= 1 ',  href: ' http://www.baidu.com/?page=1 '}

Try, when the arguments are true and false, the contents of the query field in the returned data are different.

By changing the contents of the query field, can we get something out of it and continue to learn the parse function in depth?

3. Parse function--parse the host

The third argument of the parse function is also a Boolean type, and when the argument is true, parsing will resolve the part between the "//" and the first "/" of the URL to the hostname, as shown in the following example:

var url = require (' URL '); Url.parse (' Http://www.baidu.com/news ', false,true);
Operation Result:
{protocol: ' http: ',  slashes:true,  auth:null,  host: ' www.baidu.com ',  port:null,  hostname : ' www.baidu.com ',  hash:null,  search:null,  query:null,  pathname: '/news ',  path: '/news ',  href: ' Http://www.baidu.com/news '}

Compared to the previous example, the contents of host are no longer null.

Try it out and see what's different about what's returned.

By changing the parameters, we understand that the parse function can parse the URL, get the host, query, and more, discover it, Junior!

4. Basic usage of the Format function

The Format function does the opposite of parse, and its argument is a JSON object that returns an assembled URL address, as shown in the following example:

var url = require (' URL '); Url.format ({protocol: ' http: ', hostname: ' www.baidu.com ', Port: ' + ', pathname: '/news ', query:{ Page:1}});
Operation Result:
Http://www.baidu.com/news?page=1

The fields of the parameter JSON object correspond to the JSON field one by one returned after parsing the parse function.

Try passing in an object to see what the return URL is.

Small expansion:The Format method allows you to convert a URL object to a URL string .

5. Basic usage of resolve function

The parameters of the Resolve function are two paths, the first path is the starting path or the current path, the second is the path you want to go to, and the return value is a well-assembled URL, as shown in the following example:

var url = require (' URL '); Url.resolve (' http://example.com/', '/one ')  /' Http://example.com/one ' url.resolve (' Http://example.com/one ', '/two ')//' Http://example.com/two '

Move, try resolve function.

The resolve function can be used for stitching URLs.

6. Summary of Courses

This lesson explains some common features of the URL module, if you want to learn more, you can refer to the following information:

Http://nodejs.cn/api/url

node. JS study fourth day--url processing

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.