Nodejs implementation gets the current URL address and URL various parameter values

Source: Internet
Author: User
Tags require

This article mainly introduced the Nodejs realization obtains the current URL address and the URL various parameter values, this article directly gives the code example, needs the friend to be possible to refer to under

The code is as follows:

Module HTTP URL that needs to be used

Current URL http://localhost:8888/select?aa=001&bb=002

var http = require (' http ');

var URL = require (' URL ');

Http.createserver (function (req, res) {

var arg = Url.parse (req.url). query; Method one arg => aa=001&bb=002

var arg = Url.parse (Req.url, true). Query; Method two arg => {aa: ' 001 ', BB: ' 002 '}

Console.log (ARG.AA);/return 001

Console.log (ARG.BB);/return 002

And then you can do it based on the data.

}). Listen (8888);//Set up the server and listen on the port

Get specific URL parameter values

The code is as follows:

var testurl = ' http://localhost:8888/select?aa=001&bb=002 ';

var p = url.parse (Testurl);

Console.log (P.HREF); The value to be fetched is: http://localhost:8888/select?aa=001&bb=002

Console.log (P.protocol); The value to be fetched is: http:

Console.log (p.hostname);//The value to be fetched is: locahost

Console.log (p.host);//The value to be fetched is: localhost:8888

Console.log (P.port);//The value to be fetched is: 8888

Console.log (P.path);//The value to be fetched is:/select?aa=001&bb=002

Console.log (P.hash);//The value to be taken is: null

Console.log (p.query);//The value to be fetched is: aa=001

It is noteworthy here that when the statement is var p = url.parse (Testurl, True), P.query returns an object such as: {aa: ' 001 '}, directly printing p.query returns [Object], at which point we can write : Console.log (P.QUERY.AA); The values to be taken are: 001

Console.log (p.pathname);//The value to be fetched is:/select

The following is attached to the acquisition method JS:

Current URL

The code is as follows:

http://mj_0203.0fees.net/index.php?aa=001&bb=002

document.location:http://mj_0203.0fees.net/index.php?aa=001&bb=002

Document. url:http://mj_0203.0fees.net/index.php?aa=001&bb=002

document.location.href:http://mj_0203.0fees.net/index.php?aa=001&bb=002

self.location.href:http://mj_0203.0fees.net/index.php?aa=001&bb=002

top.location.href:http://mj_0203.0fees.net/index.php?aa=001&bb=002

parent.document.location:http://mj_0203.0fees.net/index.php?aa=001&bb=002

Top.location.hostname:mj_0203.0fees.net

Location.hostname:mj_0203.0fees.net

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.