Node. js ------ (Url, QueryString, Path) Module

Source: Internet
Author: User
Tags node server

Node. js ------ (Url, QueryString, Path) Module
I. In the beginning, I analyzed the three modules in this article because they are not very long in length, and there is a dependency between them, so we will introduce and analyze the instance in sequence. For more information, see the following document: (1), "Url module": 1 var url = require ('url '); 2 var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld "; 3 console. log (typeof url. parse (queryUrl); 4 console. log (url. parse (queryUrl); 1 object // typeof 2 3 {4 protocol: 'http: ', 5 slashes: true, 6 auth: null, 7 host: 'localhost: 8888 ', 8 port: '123', 9 hostname: 'localhost', 10 hash: null, 11 search :'? Name = bigbear & memo = helloworld ', 12 query: 'name = bigbear & memo = helloworld', 13 pathname: '/bb', 14 path: '/bb? Name = bigbear & memo = helloworld ', 15 href: 'http: // localhost: 8888/bb? Name = bigbear & memo = helloworld '16} is described as follows: protocol: Request protocol host: All URL host names have been converted to lowercase, including port information auth: in the URL, the authentication information part is hostname: Host Name part, which has been converted to lower-case port: host port number part pathname: URL path part, which is located after the Host Name and before request query: 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) supplement api: "url. format (urlObj)" function: enter a URL object and return the formatted URL string. (2) The "QueryString module" "QueryString" module is used to convert URL parameter strings and parameter objects. For example: 1 var url = require ('url'); 2 var qs = require ('querystring'); 3 var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld "; 4 queryUrl = url. parse (queryUrl ). query; 5 console. log (queryUrl); 6 console. log (qs. parse (queryUrl); run the following result: name = bigbear & memo = helloworld {name: 'bigbear ', memo: 'helloworld'}: 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. if it is set to 0, the number of keys can be removed. 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 take a simple example: 1 var url = require ('url'); 2 var qs = require ('querystring'); 3 var path = require ("path "); 4 var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld "; 5 var root = path. basename (queryUrl); 6 console. log (root); // bb? Name = bigbear & memo = helloworld the last part of the returned path, separated. Copy the Code 1 var url = require ('url'); 2 var qs = require ('querystring'); 3 var path = require ("path "); 4 var queryUrl = "http: // localhost: 8888/bb? Name = bigbear & memo = helloworld "; 5 var root = path. basename (queryUrl); 6 console. log (root); // bb? Name = bigbear & memo = helloworld7 var ext = path. extname (root); 8 console. log (ext | "Not Ext Name! "); // Not Ext Name! Because there are too many APIs to copy the Code, only a few frequently used APIs are listed above. You need to carefully read the documentation. 2. Comprehensive Description of the scenario ------ the server receives different requests and performs different processing through the "Url". The Code is as follows: (1) Create "index.html" Copy code 1 <! Doctype html> 2

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.