Nodejs: Get the current url address and url parameter values _ node. js

Source: Internet
Author: User
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

Related Article

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.