node. js get/post corresponds to url/query-string common methods introduced

Source: Internet
Author: User

<, when learning Node.js--get/post requests, first look at the use of module URLs and query-string

1. Module URL usage, typically used to parse a GET request.

parse: [Function:urlparse]resolve: [Function:urlresolve]resolveobject: [Function:urlresolveobject]  Format: [Function:urlformat]URL: [Function:url]

1.1 Url.parse (str), this method is commonly used to parse the data returned by a GET request

Const URL = require (' URL '); var strurl = ' Https://user:[email protected]:8080/p/a/t/h?username=zhange&password=123456#hash '
Console.log (Url.parse (strURL));
Console.log (Url.parse (strurl,true). query);

return results

result 1:
URL {protocol:' https: ', slashes:true, Auth:' User:pass ', Host:' sub.host.com:8080 ', Port:' 8080 ', hostname:' Sub.host.com ', Hash:' #hash ', search:'? username=zhange&password=123456 ', query:' username=zhange&password=123456 ', Pathname:'/p/a/t/h ', Path:'/p/a/t/h?username=zhange&password=123456 ', href:' Https://user:[email Protected]:8080/p/a/t/h?username=zhange&password=123456#hash ' }

Result 2:
{username: ' zhange ', Password: ' 123456 '}

Returns an object that describes the argument parsing

HREF: The full original URL that was parsed. Protocol name and hostname have been converted to lowercase
Protocol: Request protocol, lowercase
Host:url hostname has all been converted to lowercase, including port information
Authentication Information section in Auth:url
Hostname: hostname part of host, converted to lowercase
Port: The port number portion of the host
The path portion of the Pathname:url, before the host name is requested for the query
The "Query string" section of Search:url, including the question mark at the beginning
Path:pathname and search are linked together.
Query: The parameter part of the inquiry string (the string after the question mark)
Hash:url "#" after section (including # symbol)

1.2 Url.format (STR) is a 1.1 url.parse (str) Inverse usage that converts a given object to a string

Const URL = require (' url '= {    ' https: ',    true,    ' User:pass ') ,    ' sub.host.com:8080 ',    ' 8080 ',    ' sub.host.com    ', ' #hash ',     '? query=string ',        ' query=string ', '/p/a/t/h    ', '/p/a /t/h?query=string ',    ' https://user:[email protected]:8080/p/a/t/h?query=string#hash '= Url.format (obj);

The results obtained

HTTPS://user:[email protected]:8080/p/a/t/h?query=string#hash

2. Module query-string usage, typically used to parse the data of a POST request.

2.1 Querystring.parse (Str,separator,eq,options)

Parse this method is to deserialize a string into an object.

Parameter: str refers to a string that needs to be deserialized;

Separator (can be saved) refers to the character or string used to split the STR string, and the default value is "&";

EQ (can be saved) refers to the character or string used to divide the key and value, and the default value is "=";

Options (can be saved) This parameter is an object that can set both Maxkeys and decodeURIComponent properties:

A. Maxkeys: Pass in a number type that specifies the maximum value of the resolution key-value pair, the default value is 1000, and if it is set to 0 o'clock, the resolution limit is canceled;

B. decodeURIComponent: A function is passed in to decode the string containing%, the default value is Querystring.unescape. In the official API example, using Gbkdecodeuricomponent This method will error, showing gbkdecodeuricomponent is no defined, This is because the Gbkdecodeuricomponent method needs to be defined before it can be used. In the API also wrote the assuming gbkdecodeuricomponent function already exists ... This sentence means "Suppose this gbkdecodeuricomponent method already exists".

Const QUERYSTRING = require (' query-string '); var str = ' username=zhange&password=123456&type=girl '; Console.log (Querystring.parse (str));

return results

{password: ' 123456 ', type: ' Girl ', Username: ' Zhange '}

2.2 querystring.stringify (obj,separator,eq,options)

Stringify This method is to serialize an object into a string, as opposed to querystring.parse.

Parameter: obj refers to the object that needs to be serialized

Separator (can be saved) the character or string used to connect key-value pairs, with the default value of "&";

EQ (can save) the character or string that is used to connect the key and value, and the default value is "=";

Options (can be saved) pass in an object that can set encodeuricomponent this property: encodeURIComponent: The type of the value is function, you can convert an unsafe URL string into a percentage form, The default value is Querystring.escape ().

Querystring.stringify ({foo: ' Bar ', Baz: [' Qux ', ' Quux '], Corge: ' }); // back to ' foo=bar&baz=qux&baz=quux&corge= '  ' Bar ', Baz: ' Qux '}, '; ', ': '); // back to ' Foo:bar;baz:qux '

node. js get/post corresponds to url/query-string common methods introduced

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.