A simple solution for parsing complex URL's. This utility is based on the native url module exposed by node js. This is made while keeping Node in mind so as to provide an easy access to URL constituents anyone who uses it. There are utility functions in this module that is handy while using it along with other node modules.
It's pretty easy to install. Just type the following in order to get this as a dependency.
$ npm install url-parser -D
After this you can import this in any project and create a new instance & pass the URL as the parameter that you would like to parse. This can be done as follows:-
var URL = require('url-parser'); var urlObj = new URL(YOUR_URL); //After this step you can use the urlObj and call the functions that you want. The list is mentioned below
This utility has multiple function exposing each part of the URL. Namely this can constitute of the following parts:-
Gives you the protocol of the URL that is passes as the argument in the constructor for example:-
//This will return http: or https: urlObj.getProtocol();
Tells you whether the URL ends with a trailing slash or not
/*This will give you a boolean variable true/false letting you know whether there is a trailing slash or not*/ urlObj.getSlashes();
This tells whether the URL carries Auth or not. If not the value will be null
Gives you the host from the URL. This would represent the Host along with the port if available.
Gets the port Number. If there is no port number it would be null or undefined. You can consider it to be 80 in such cases. If the protocol is https or if the site is SSL enabled then you can consider it as 443
This will give you just the domain name from the URL.
Gets you query parameters along with the ? symbol.
Gets you the query parameters only without the ? symbol.
Gets the context path from the URL.
Gets the entire path along with the context path excluding the hostname.
Returns the full URL that is being queried for or the URL that is being passed as the parameter to the constructor.
Copyright (c) 2016 Shiv Kumar Ganesh