Tuesday, March 31, 2020

Node.js URL Module

Node.js URL Module


The Node.js URL Module is provided to perform operations with address URLs. We can split a web address into manageable parts. A URL string may carry information including the protocol such as https, the hostname, query string values and more.

This is a built-in module, so no need to install this module exclusively. It can be included using the require() method.

var url = require('url');

URL Strings and URL Objects


A URL string is a structured string composed of several meaningful components. We can parse a URL string to get the URL object possessing properties for each of these components.

We can parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties:

            var url = require('url');
      var adr = 'http://localhost:3000/index.html?year=2020&month=March';
      var u = url.parse(adr, true);
      console.log(u.host);
      console.log(u.pathname);
      console.log(u.search);
      var qdata = u.query;
      console.log(qdata.month);
      /* Output must be,

      localhost:3000
      /index.html
      ?year=2020&month=March
      March
      */


Node.js url.format() method


We can create a URL from an object using the format() method. The object must contain the protocol hostname and the path property. This object may also contain other properties like hash links and query strings.


             const url = require('url');
      const page = url.format({
              protocol: 'https',
              hostname: 'www.programminghunk.com',
              pathname: 'p/nodejs.html',
              hash: 'Overview'
          });
      console.log(page);
      /*
https://www.programminghunk.com/p/nodejs.html#Overview
      */

The URL constructor


The URL constructor creates a new URL object by parsing the input relative to the base. If the base is passed as a string, it will be parsed equivalent to new URL(base). We can visualize components of a URL as,

URL Components
URL Components
The components of the URL can be accessed as the properties of the URL object. All the properties of a URL object can be listed as,

  • url_obj.auth
  • url_obj.hash
  • url_obj.host
  • url_obj.hostname
  • url_obj.href
  • url_obj.path
  • url_obj.pathname
  • url_obj.port
  • url_obj.protocol
  • url_obj.query
  • url_obj.search
  • url_obj.slashes


           let url = require('url');
      // Constructor
      let mypage = new URL('http://pushpendra:1234@localhost:3000/Node/tutorials?k=first#intro');
      
      console.log("origin "+mypage.origin);
      console.log("protocol "+mypage.protocol);
      console.log("username "+mypage.username);
      console.log("password "+mypage.password);
      console.log("Host "+mypage.host);
      console.log("hostname "+mypage.hostname);
      console.log("Port "+mypage.port);
      console.log("pathname "+mypage.pathname);
      console.log("search "+mypage.search);
      console.log("hash "+mypage.hash);
      console.log("href "+mypage.href);
      console.log("toString"+mypage.toString());
      /*
      origin http://localhost:3000
      protocol http:
      username pushpendra
      password 1234
      Host localhost:3000
      hostname localhost
      Port 3000
      pathname /Node/tutorials
      search ?k=first
      hash #intro
      href http://pushpendra:1234@localhost:3000/Node/tutorials?k=first#intro
      toStringhttp://pushpendra:1234@localhost:3000/Node/tutorials?k=first#intro
      */