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