This article mainly introduces how to obtain the current url address and url parameters in nodejs. This article provides code examples. For more information, see
The Code is as follows:
// The http url of the required Module
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 the data according to the obtained data.
}). Listen (8888); // create a server and listen to 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 obtained value is: http: // localhost: 8888/select? Aa = 001 & bb = 002
Console. log (p. protocol); // The obtained value is: http:
Console. log (p. hostname); // the value obtained is locahost.
Console. log (p. host); // The obtained value is: localhost: 8888
Console. log (p. port); // the value obtained is 8888.
Console. log (p. path); // The obtained value is:/select? Aa = 001 & bb = 002
Console. log (p. hash); // The obtained value is: null.
Console. log (p. query); // The obtained value is aa = 001.
It is worth noting that when the statement is var p = URL. parse (testUrl, true), p. query returns an object such as: {aa: '001'} and prints p directly. query Returns [object Object]. In this case, we can write: console. log (p. query. aa); // The obtained value is: 001
Console. log (p. pathname); // the value obtained is:/select
The following describes how to obtain 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.doc ument. 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