node. JS URL Module

Source: Internet
Author: User

Url

    • Url.parse (urlstr[, parsequerystring][, Slashesdenotehost])
    • Url.format (Urlobj)
    • Url.resolve (from, to)

Specific address: http://nodejs.cn/api/url.html

3 Modes of reference

C:\Documents and Settings\administrator\webstormprojects\untitled3>node

Enter node environment first

> URL
{parse: [Function:urlparse],
Resolve: [Function:urlresolve],
Resolveobject: [Function:urlresolveobject],
Format: [Function:urlformat],
URL: [Function:url]}

Call URL

Parse Parse:

    • Url.parse (urlstr[, parsequerystring][, Slashesdenotehost])

> Url.parse (' http://www.imooc.com/video/6710 ')

Build Item:
{protocol: ' http: ',
Slashes:true,
Auth:null,
Host: ' Www.imooc.com ',
Port:null,
Hostname: ' www.imooc.com ',
Hash:null,
Search:null,
Query:null,
Pathname: '/video/6710 ',
Path: '/video/6710 ',
HREF: ' http://www.imooc.com/video/6710 '}

Format parsing: The information you give generates a URL

    • Url.format (Urlobj)

> Url.format ({protocol: ' http: ',
... slashes:true,
... auth:null,
... host: ' Www.imooc.com ',
... port:null,
... hostname: ' www.imooc.com ',
... hash:null,
... search:null,
... query:null,
... pathname: '/video/6710 ',
... path: '/video/6710 ',
... href: ' http://www.imooc.com/video/6710 '}
... )
Build item: ' http://www.imooc.com/video/6710 '

Resolve parsing

    • Url.resolve (from, to)

> url.resolve (' http://imooc.com/', '/course/list ')
Build item: ' Http://imooc.com/course/list '

Application of Parse

> Url.parse (' Http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1 ')
{protocol: ' http: ',
Slashes:true,
Auth:null,
Host: ' imooc.com:8080 ',
Port: ' 8080 ',
Hostname: ' imooc.com ',
Hash: ' #dloor1 ',
Search: '? From=scott&&course=node ',
query: ' From=scott&&course=node ',
Pathname: '/curse/list ',
Path: '/curse/list?from=scott&&course=node ',
HREF: ' Http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1 '}

> Url.parse (' Http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1 ',true)
{protocol: ' http: ',
Slashes:true,
Auth:null,
Host: ' imooc.com:8080 ',
Port: ' 8080 ',
Hostname: ' imooc.com ',
Hash: ' #dloor1 ',
Search: '? From=scott&&course=node ',
query: {from: ' Scott ', ': ', ' Course: ' node '}, //query is parsed as an object
Pathname: '/curse/list ',
Path: '/curse/list?from=scott&&course=node ',
HREF: ' Http://imooc.com:8080/curse/list?from=scott&&course=node#dloor1 '}

Compare it to each other and see what's different.

> Url.parse ('//imooc.com/curse/liast ', true)
{protocol:null,
Slashes:null,
Auth:null,
Host:null,
Port:null,
Hostname:null,
Hash:null,
Search: ",
Query: {},
Pathname: '//imooc.com/curse/liast ',
Path: '//imooc.com/curse/liast ',
HREF: '//imooc.com/curse/liast '}

> Url.parse ('//imooc.com/curse/liast ', True,true)
{protocol:null,
Slashes:true,
Auth:null,
host: ' Imooc.com ',
Port:null,
Hostname: ' imooc.com ',
Hash:null,
Search: ",
Query: {},
Pathname: '/curse/liast ',
Path: '/curse/liast ',
HREF: '//imooc.com/curse/liast '}

Protocol: Protocol
Slashes: whether there is a double slash of the Protocol
Auth
Host: Domain/IP address
Port: Ports
Hostname: Host Name
Hash: Target an anchor point
Search: Query string parameters
Query: A parameter string that is sent to http with an argument = number branch Open
Pathname: Access Resource path name
Path: Paths
HREF: Hypertext link

node. JS 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.