Node JS URL Module

Let’s start

Today we will learn about Node Js in URL Module

URL module is the built-in node js module, it is used for split URL address and you can read this part

 Property   Description 
 .href  Provide us the complete URL string
 .host  Give us the Hostname and Port number
 .hostname  Give us the Host name of the URL
 .searchparam  return query string value
 .pathname  Provide Pathname, Port, and Pathname
 .auth  Authorization part URL
 .protocol  protocol use for request
 .search  Its return query string
 .port  Give us Port number of a specific URL

as you can see from the above screen explain about URL module properties

Below code snipped return all URL properties

Index.js

const http = require('http')
const URL = require('url')


http.createServer((req,res)=>{
    let urlString = URL.parse(req.url)
    console.log(urlString);
}).listen(7600)

First, run the index.js file then localhost:7600 this URL run on your browser nd you will get the below output

  • href:-  href property return complete URL string along with  query string key value
http.createServer((req,res)=>{
    let urlString = URL.parse(req.url,true)
    console.log(urlString.href);
}).listen(7600)

Run on browser

localhost:7600/abc.html OR localhost:7600/abc.html?search=node

output:

  • host and hostname:-There is a major difference between in both host property return hostname with port number and hostname return only hostname
http.createServer((req,res)=>{
    const urlString = new URL('https://www.thecodehub.com:7600/home.html');  
    console.log(urlString.host);
    console.log(urlString.hostname);
}).listen(7600)

output:

  • pathname and searchParams:- pathname return path and searchParams return query string
http.createServer((req,res)=>{
    const urlString = new URL('https://www.thecodehub.com:7600/home.html?search=node');  
    console.log(urlString.pathname);
    console.log(urlString.searchParams);
}).listen(7600)

output:

  • Search:-Search property same as searchParams but both in a major difference  search return query string along [?],you will see output below
http.createServer((req,res)=>{
    const urlString = new URL('https://www.thecodehub.com:7600/home.html?search=node&page=1');  
    console.log(urlString.search);
}).listen(7600)

output:

  • Port: Port property return Port  associated with a URL
http.createServer((req,res)=>{
    const urlString = new URL('https://www.thecodehub.com:7600/home.html?search=node&page=1');  
    console.log(urlString.port);
}).listen(7600)

output:

  • Protocol: Protocol property used to get a specific request just like a Http and https
http.createServer((req,res)=>{
    const urlString = new URL('https://www.thecodehub.com:7600/home.html?search=node&page=1');  
    console.log(urlString.protocol);
}).listen(7600)

output:

  • auth: its returns only username and password
http.createServer((req,res)=>{
    let url = 'https://username=abc:password=thecodehubs@www.thecodehubs.com/';
    var q = URL.parse(url, true);
    console.log(q.auth);
}).listen(7600)

output:

if you have any queries comment on contact me

To follow my other node.js articles you can go through the following links:

Node js Fs module : https://www.thecodehubs.com/node-js-fs-module

Node js in express validator:- https://www.thecodehubs.com/express-validator-in-node-js

Submit a Comment

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

Subscribe

Select Categories