Node.js URL Module

Node.js URL Module

URL Module is used to parse an address into host, pathname, search and query string.

parse method of URL module is used to get url properties.

Syntax :

<%

var url = require('url');

 url.parse();

%>

Example :

var url = require('url');
var student = 'http://localhost:5555/getData.html?city=ahmedabad&country=india';
var data = url.parse(student, true);

console.log(data.host); //returns 'localhost:5555'
console.log(data.pathname); //returns '/getData.html'
console.log(data.search); //returns '?city=ahmedabad&country=india'

var objdata = data.query; //returns an object: { city: ahmedabad, country: 'india' }
console.log(objdata.country); //returns 'india'

 

 

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

27290