ip-addressvsuri.js
The "ip-address" library is a Node.js library that provides utility functions for working with IP addresses, similar to "ip" library. It provides functions for parsing, validating, and converting IP addresses between different formats, both IPv4 and IPv6.
URI.js is a lightweight JavaScript library for working with URLs and URIs in Node.js and the browser. It provides a simple and consistent interface for parsing, manipulating, and building URLs and URIs.
Example Use
const IPAddress = require("ip-address").Address;
// slice and manipulate addresses:
const ipv4 = new IPAddress("192.168.1.1");
console.log(ipv4.addressMinusSuffix); // '192.168.1'
// validate addresses:
console.log(IPAddress.isValid("192.168.1.1")); // true
console.log(IPAddress.isValid("2001:0db8:85a3:0000:0000:8a2e:0370:7334")); // true
const URI = require('uri-js');
// parse url for values
const parsedUrl = URI.parse("https://www.example.com/search?q=query+string#fragment");
console.log(parsedUrl);
/* Output:
{
scheme: 'https',
authority: 'www.example.com',
path: '/search',
query: 'q=query+string',
fragment: 'fragment'
}
*/
// create url from values
const urlComponents = {
scheme: 'https',
authority: 'www.example.com',
path: '/search',
query: 'q=query+string',
fragment: 'fragment'
};
const url = URI.serialize(urlComponents);
console.log(url);
// Output: 'https://www.example.com/search?q=query+string#fragment'