url-parse-utility

1.1.0 • Public • Published

Url-Parser

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.

Installation

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

API

This utility has multiple function exposing each part of the URL. Namely this can constitute of the following parts:-

  1. Protocol
  2. Slashes
  3. Auth
  4. Host
  5. Port
  6. Hostname
  7. Hash
  8. Search
  9. Query
  10. Pathname
  11. Path
  12. Href

getProtocol()

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();

getSlashes()

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();

getAuth()

This tells whether the URL carries Auth or not. If not the value will be null

urlObj.getAuth();

getHost()

Gives you the host from the URL. This would represent the Host along with the port if available.

urlObj.getHost();

getPort()

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

urlObj.getPort();

getHostname()

This will give you just the domain name from the URL.

urlObj.getHostname();

getSearch()

Gets you query parameters along with the ? symbol.

urlObj.getSearch();

getQuery()

Gets you the query parameters only without the ? symbol.

urlObj.gerQuery();

getPathname()

Gets the context path from the URL.

urlObj.getPathname();

getPath()

Gets the entire path along with the context path excluding the hostname.

urlObj.getPath()

getHref()

Returns the full URL that is being queried for or the URL that is being passed as the parameter to the constructor.

urlObj.getHref()

License

MIT License

Copyright (c) 2016 Shiv Kumar Ganesh

Package Sidebar

Install

npm i url-parse-utility

Weekly Downloads

6

Version

1.1.0

License

MIT

Last publish

Collaborators

  • shivkumarganesh