node. js Initial Knowledge URL module

Source: Internet
Author: User

The URL module provides some tools for parsing URLs

var url = require (' URL ');


A URL is a structured string that contains a number of meaningful parts. When the parsing is complete, a URL object (containing the properties of each section) is returned.


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8C/6F/wKioL1hstFLyfdG-AABGNZGvyz4802.png "title=" Qq20170104162326.png "alt=" Wkiol1hstflyfdg-aabgnzgvyz4802.png "/>


Urlobject.href

The href attribute refers to the parsed complete URL string, including the protocol name, host name, and other parts, and is converted to lowercase letters.

For example:' http://user:[email protected]:8080/p/a/t/h?query=string#hash '



Urlobject.protocol

The Protocol property refers to the protocol name of the lowercase format URL

For example:' http: '



Urlobject.slashes

The slashes property is a Boolean value that is true if the protocol name is followed by two//



Urlobject.host

The host property is the hostname of the entire lowercase URL, and also includes the port number

For example:' host.com:8080 '



Urlobject.auth

The Auth property is the user name and password part of the URL

For example:' User:pass '



Urlobject.hostname

The Hostname property is the host name of the lowercase URL, excluding the port number

For example:' host.com '



Urlobject.port

The Port property is the port number, and the number part of the host property

For example:' 8080 '



Urlobject.pathname

The Pathname property consists of the full path part of the URL, after the host property (including the port number) and before the query or hash attribute, by the. or # to define

For example:' host.com '




Urlobject.search

The search property consists of the entire query string part of the URL, containing the "? "Symbol

For example:'? query=string '




Urlobject.path

The Path property is a concatenation of the pathname section and the search section

For example:'/p/a/t/h?query=string '




Urlobject.query

The query property is not included with the "? The query string for the symbol or an object returned by the parse () method of the QueryString module.

For example: ‘query=string‘ or{‘query‘: ‘string‘}




Urlobject.hash

The hash attribute refers to the anchor point, which contains the part of the "#" symbol in the URL.

For example:' #hash '


Now you can see the parts of the URL being parsed by a section of JS code.

var http = require (' http '), var url = require (' URL '), var server = Http.createserver (function (req,res) {//url.parse () receive    A URL string that parses and returns a URL object var result = Url.parse (' Http://user:[email protected]:8080/p/a/t/h?query=string#hash ');    Console.log (result); Res.end ();}); Server.listen (4000, ' 127.0.0.1 ');


Open the browser, enter "127.0.0.1:4000" in the Address bar, you can view the console print out: (Here is the webstorm, if not, you need to use the command-line tool to hang up the server first)

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8C/73/wKiom1hsu96z32qGAABZ_3pGPAM362.png "title=" Qq20170104161544.png "alt=" Wkiom1hsu96z32qgaabz_3pgpam362.png "/>


Url.parse () If the second argument is true, then all queries can be changed to object

var http = require (' http '), var url = require (' URL '), var server = Http.createserver (function (req,res) {//url.parse () receive    A URL string that parses and returns a URL object var result = Url.parse (' Http://user:[email protected]:8080/p/a/t/h?query=string#hash ', true);    Console.log (result); Res.end ();}); Server.listen (4000, ' 127.0.0.1 ');


View the query in the Print results section

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8C/70/wKioL1hsu9Dxg4QDAABjMmlhicE268.png "title=" Qq20170104161733.png "alt=" Wkiol1hsu9dxg4qdaabjmmlhice268.png "/>



Based on the previous concept, we try to do a small example. Create a new form page, enter the user name, age, gender, submit to 127.0.0.1:5000, and output the submitted information in the page


HTML code:

<! Doctype html>



JS Code:

var http = require (' http '), var url = require (' URL '), var server = Http.createserver (function (req,res) {//Get query part, second parameter is TR    UE, so get an object; otherwise, get a string var Queryobject = Url.parse (Req.url, true). Query;    Get user name var name = Queryobject.name;    Get age var = queryobject.age;    Get gender var gender = Queryobject.gender; Res.end (' Server received the form request: ' + name + ': ' + Age + ': ' +gender)}); Server.listen (5000, ' 127.0.0.1 ');



Double click to open the client's form page, enter the information, click the "Submit" button

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8C/74/wKiom1hswEGSJnkTAAAcHD5GFjI012.png "title=" Qq20170104172746.png "alt=" Wkiom1hswegsjnktaaachd5gfji012.png "/>


The user information was obtained after submission:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8C/74/wKiom1hswFeDZ7hKAAAbEI6_gX4780.png "title=" Qq20170104172400.png "alt=" Wkiom1hswfedz7hkaaabei6_gx4780.png "/>

This article is from the "Dapengtalk" blog, make sure to keep this source http://dapengtalk.blog.51cto.com/11549574/1889020

node. js Initial Knowledge URL module

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.