Node. js

Source: Internet
Author: User
This article mainly introduces the method for operating URLs in the nodejsURL module. This article describes the parse method, format method, querystring method, and so on. For more information, see Url module

The url module has a high usage rate when processing HTTP requests because it allows parsing, generating, and splicing URLs. First, let's take a look at the components of a complete URL.

The code is as follows:


Href
-----------------------------------------------------------------
Host path
-------------------------------------------
Http: // user: pass @ host.com: 8080/p/a/t/h? Query = string # hash
----------------------------------------------------
Protocol auth hostname port pathname search hash
------------
Query


We can use the. parse method to convert a URL string to a URL object, as shown in the following example.

The code is as follows:


Url. parse ('http: // user: pass@host.com: 8080/p/a/t/h? Query = string # hash ');
/* =>
{Protocol: 'http :',
Auth: 'User: pass ',
Host: 'host. com: 8080 ',
Port: '123 ',
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: pass@host.com: 8080/p/a/t/h? Query = string # hash '}
*/


The transfer to. parse method is not necessarily a complete URL. for example, in the HTTP server callback function, request. url does not contain the protocol header and domain name, but can also be parsed using the. parse method.

The code is as follows:


Http. createServer (function (request, response ){
Var tmp = request. url; // => "/foo/bar? A = B"
Url. parse (tmp );
/* =>
{Protocol: null,
Slashes: null,
Auth: null,
Host: null,
Port: null,
Hostname: null,
Hash: null,
Search :'? A = B ',
Query: 'A = B ',
Pathname: '/foo/bar ',
Path: '/foo/bar? A = B ',
Href: '/foo/bar? A = B '}
*/
}). Listen (80 );


The. parse method also supports the second and third Boolean type optional parameters. When the second parameter is true, in the URL object returned by this method, the query field is no longer a string, but a parameter object converted by the querystring module. When the third parameter is true, this method can correctly parse URLs without protocol headers, such as // www.example.com/foo/bar.

In turn, the format method allows you to convert a URL object to a URL string, as shown in the following example.

The code is as follows:


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'
*/


In addition, the. resolve method can be used to splice URLs, as shown in the following example.

The code is as follows:


Url. resolve ('http: // www.example.com/foo/bar', '../Baz ');
/* =>

Http://www.example.com/baz

*/


Query String

The querystring module is used to convert URL parameter strings and parameter objects. The following is an example.

The code is as follows:


Querystring. parse ('foo = bar & baz = qux & baz = quux & corge ');
/* =>
{Foo: 'bar', baz: ['qux', 'quux '], corge :''}
*/

Querystring. stringify ({foo: 'bar', baz: ['qux', 'quux '], corge :''});
/* =>
'Foo = bar & baz = qux & baz = quux & corge ='
*/

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.