Home > Web Front-end > JS Tutorial > nodejs implements obtaining the current url address and various url parameter values_node.js

nodejs implements obtaining the current url address and various url parameter values_node.js

WBOY
Release: 2016-05-16 15:52:44
Original
2429 people have browsed it

Copy code The code is as follows:

//Module to be used http url
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 1 arg => aa=001&bb=002
var arg = url.parse(req.url, true).query; //Method 2 arg => { aa: '001', bb: '002' }
console.log(arg.aa);//Return 001
console.log(arg.bb);//Return 002
//Then you can process it based on the obtained data

}).listen(8888);//Establish a server and listen on the port

Get specific url parameter value

Copy code 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 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 printing p.query directly returns [ object Object], then we can write like this: console.log(p.query.aa); //The value obtained is: 001
console.log( p.pathname);//The value obtained is: /select

Attached below is how to obtain js:
Current URL

Copy code 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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template