Skip to content

ip-addressvsuri.js

MIT 34 1 526
61.0 million (month) May 05 2015 9.0.5(1 year, 21 days ago)
6,259 2 104 MIT
Jun 18 2013 694 (month) 0.1.3(2 years ago)

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'

Alternatives / Similar


Was this page helpful?