Node. js (Url, QueryString, Path) module _ node. js

Source: Internet
Author: User
Tags node server
Today, let's take a look at the related operation methods of the path in nodejs. In our development process, the paths are mainly URL paths used in browsers. Of course, this also contains operations related to QueryString query. The other is the disk path, it is mainly used for file operations. We call it Path, so I put them together for summarization. I. Opening Analysis

This article uses these three modules together to explain the reason that they are not very long in length, and there is a dependency between them, so we will introduce them in sequence and analyze the instances. Please refer to the following documents for more information:

(1), "Url module"

Here is a small chestnut:

The Code is as follows:


Var url = require ('url ');
Var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld ";
Console. log (typeof url. parse (queryUrl ));
Console. log (url. parse (queryUrl ));

Running result:

The Code is as follows:


Object // typeof

{
Protocol: 'http :',
Slashes: true,
Auth: null,
Host: 'localhost: 8888 ',
Port: '123 ',
Hostname: 'localhost ',
Hash: null,
Search :'? Name = bigbear & memo = helloworld ',
Query: 'name = bigbear & memo = helloworld ',
Pathname: '/bb ',
Path: '/bb? Name = bigbear & memo = helloworld ',
Href: 'http: // localhost: 8888/bb? Name = bigbear & memo = helloworld'
}

It is described as follows:

Protocol: Request protocol
Host: All URL host names have been converted to lowercase, including port information.
Auth: authentication information in the URL
Hostname: the host name, which has been converted to lowercase.
Port: port Number of the host
Pathname: the URL path, before querying the request after the Host Name
Search: the "query string" section of the URL, including the question mark at the beginning.
Path: the pathname and search are connected together.
Query: query the parameter part of a string (the part after the question mark), or use querystring. parse () to parse the returned object.
Hash: the part after the URL "#" (including the # symbol)

Additional api: "url. format (urlObj )"
  

Purpose: enter a URL object and return the formatted URL string.

(2), "QueryString module"

The "QueryString" module is used to convert URL parameter strings and parameter objects, as shown below:

The Code is as follows:


Var url = require ('url ');
Var qs = require ('querystring ');
Var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld ";
QueryUrl = url. parse (queryUrl). query;
Console. log (queryUrl );
Console. log (qs. parse (queryUrl ));

The running result is as follows:

The Code is as follows:


Name = bigbear & memo = helloworld
{
Name: 'biginar ',
Memo: 'helloworld'
}

Supplementary api:

Querystring. stringify (obj, [sep], [eq]) ------ serialize an object to a query string.

You can select whether to overwrite the default delimiter ('&') and the delimiter ('= ').

Querystring. stringify ({foo: 'bar', baz: 'qux'}, ';', ':') // return the following string: 'foo: bar; baz: qux'

Querystring. parse (str, [sep], [eq], [options]) ------ deserializes a query string into an object. You can select whether to overwrite the default delimiter ('&') and the delimiter ('= ').
  
The options object may contain the maxKeys attribute (1000 by default). It can be used to limit the number of keys processed. Setting it to 0 can remove the number of keys.
  
Example: querystring. parse ('foo = bar & baz = qux & baz = quux & corge ') // {foo: 'bar', baz: ['qux', 'quux'], corge :''}

(3), "Path module"

This module contains a set of tools used to process and convert file paths. Almost all methods only convert strings. The file system does not check whether the path is true and valid.

Let's start with a simple example:

The Code is as follows:


Var url = require ('url ');
Var qs = require ('querystring ');
Var path = require ("path ");
Var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld ";
Var root = path. basename (queryUrl );
Console. log (root); // bb? Name = bigbear & memo = helloworld

The last part of the returned path, separated.

The Code is as follows:


Var url = require ('url ');
Var qs = require ('querystring ');
Var path = require ("path ");
Var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld ";
Var root = path. basename (queryUrl );
Console. log (root); // bb? Name = bigbear & memo = helloworld
Var ext = path. extname (root );
Console. log (ext | "Not Ext Name! "); // Not Ext Name!


Because there are too many APIs, the above are just a few frequently used ones. You need to carefully read the documentation.

2. Integrated chestnuts

Scenario Description ------ the server processes different requests in different situations through the Url. The Code is as follows:

(1) Create "index.html"

The Code is as follows:





Bigbear



Related Article

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.