The nodejs implementation gets the current url address and various url parameter values

  • 2020-06-19 09:49:50
  • OfStack


// The modules you need to use http   url
The 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;  // methods 1arg => aa=001&bb=002
   var arg = url.parse(req.url, true).query;  // methods 2arg => { aa: '001', bb: '002' }
   console.log(arg.aa);// return 001
   console.log(arg.bb);// return 002
   // Then you can process the data as you get it }).listen(8888);// Set up the server and listen on the port

Gets the value of the specific url parameter


var testUrl =  'http://localhost:8888/select?aa=001&bb=002';
var p = URL.parse(testUrl);
console.log(p.href); // The value obtained is: http://localhost:8888/select?aa=001&bb=002
console.log(p.protocol); // The value obtained is: http:
console.log( p.hostname);// The value obtained is: locahost
console.log(p.host);// The value obtained is: localhost:8888
console.log(p.port);// The value obtained is: 8888
console.log(p.path);// The value obtained is: /select?aa=001&bb=002
console.log(p.hash);// The value obtained is: null
console.log(p.query);// The value obtained is: aa=001

It is worth noting here that when the statement is var p = URL.parse (testUrl, true), p.query returns an object such as {aa:'001'} and print p.query [object]. The value obtained is: 001
console. log (p. pathname); // The value obtained is: /select

The method of getting js is attached below:
The current URL


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


Related articles: