Nodejs URL Detailed

Source: Internet
Author: User

1 We can use the . Parse method to convert a URL string to a URL object

For example:

Url.parse (' Http://user:[email protected]:8080/p/a/t/h?query=string#hash ');
/* =
{protocol: ' http: ',
Auth: ' User:pass ',
Host: ' host.com:8080 ',
Port: ' 8080 ',
Hostname: ' host.com ',
Hash: ' #hash ',
Search: '? query=string ',
Query: ' query=string ',
Pathname: '/p/a/t/h ',
Path: '/p/a/t/h?query=string ',
HREF: ' Http://user:[email protected]:8080/p/a/t/h?query=string#hash '}
*/

Url.parse (Urlstr, [parsequerystring], [Slashesdenotehost])

Receive parameters:

Urlstr URL string

When parsequerystring is true, query strings are parsed using the query module, which defaults to False

Slashesdenotehost

A string that defaults to False,//foo/bar form will be interpreted as {pathname: '//foo/bar '}

If the string is set to True,//foo/bar form, it will be interpreted as {host: ' foo ', pathname: '/bar '}

Example:

var url = require (' URL ');
var a = Url.parse (' http://example.com:8080/one?a= Index&t=article&m=default ');
Console.log (a);
 
//output:
{
    protocol: ' http ',
    auth:null,
 &N bsp;  host: ' example.com:8080 ',
    port: ' 8080 ',
    hostname: ' example.c Om ',
    hash:null,
    search: '? A=index&t=article&m=default ',
&N bsp;   query: ' A=index&t=article&m=default ',
    pathname: '/one ',
 & nbsp;  path: '/one?a=index&t=article&m=default ',
    href: ' http://example.com:8080 /one?a=index&t=article&m=default '
}

If parsequerystring is set to true , query in the URL object becomes an object, such as: Query:{a: "Index", T:: "article", M: "Default"}

2 . Resolve method can be used for stitching URLs

Url.resolve (' Http://www.example.com/foo/bar ', ' ... /baz ');
/* =

Http://www.example.com/baz

*/

3 in turn, the. Format method allows you to convert a URL object to a URL string

Url.format ({
Protocol: ' http: ',
Host: ' www.example.com ',
Pathname: '/p/a/t/h ',
Search: ' query=string '
});
/* =
' Http://www.example.com/p/a/t/h?query=string '
*/

URL parameter Description :

{

Protocol: ' http: ', Slashes:true, Auth:null, Host: ' localhost:8888 ', Port: ' 8888 ', hostname: ' Localho St ', Hash:null, search: '? Name=bigbear&memo=helloworld ', query: ' Name=bigbear&memo=helloworld ', p Athname: '/bb ', path: '/bb?name=bigbear&memo=helloworld ', href: ' Http://localhost:8888/bb?name=bigbear&mem O=helloworld '} protocol: the request protocol Host:url hostname has all been converted to lowercase, including the port information Auth:url the authentication Information section hostname: Host name part, converted to lowercase port: primary   The port number section of the machine Pathname:url the path portion of the query string section before requesting the query after the hostname, including the question mark at the beginning of the Search:url.   Path:pathname and search are linked together.   Query: The parameter part of the queried string (the string after the question mark), or the object returned after parsing with Querystring.parse (). Hash:url "#" after section (including # symbol)

Nodejs URL Detailed

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.