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'
}

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers