url module का use किसी url को readable form में split करता है , जिससे हम अपनी need के according work कर सकें।

NodeJS url.parse()

url.parse() method url को as a Object return करता है , जिसमे url के हर part को property : value pair में encapsulate करता है।
See Example :

Copy Fullscreen Close FullscreenRun
// load url module.
const url = require('url');
const demo_url = 'http://localhost:8080/user?name=rahul&age=24&address=india';
var url_obj = url.parse(demo_url, true);

console.log('host : '+ url_obj.host);
console.log('path : '+ url_obj.pathname);
console.log('query string : ' + url_obj.search); //returns '?year=2017&month=february'

// we can also access query string as a property useing qery proprty:
var query = url_obj.query;
console.log('Name : '+ query.name);
console.log('Age : '+ query.age);
console.log('Address : '+ query.address);
C:\Users\HP\Desktop\workspace\nodejs>node app.js
host : localhost:8080
path : /user
query string : ?name=rahul&age=24&address=india
Name : rahul
Age : 24
Address : india

हालाँकि किसी भी url के बारे में completely information जानने के लिए url Object को console में print कराकर देख सकते हैं , इससे आपको हर एक property और method के बारे में पता चल जायगा।

Copy Fullscreen Close FullscreenRun
const url = require('url');
const demo_url = 'http://localhost:8080/user?name=rahul&age=24&address=india';
var url_obj = url.parse(demo_url, true);
console.log(url_obj);
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'localhost:8080',
  port: '8080',
  hostname: 'localhost',
  hash: null,
  search: '?name=rahul&age=24&address=india',
  query: [Object: null prototype] {
    name: 'rahul',
    age: '24',
    address: 'india'
  },
  pathname: '/user',
  path: '/user?name=rahul&age=24&address=india',
  href: 'http://localhost:8080/user?name=rahul&age=24&address=india'
}

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook