Ultimate Guide To uri-js for Efficient URI Parsing and Manipulation

Welcome to the Ultimate Guide to uri-js

uri-js is a powerful library that provides a set of utilities for URI parsing, validation, and manipulation in JavaScript. This guide covers an introduction to the library along with dozens of useful API explanations and code snippets to help you get the most out of uri-js.

What is uri-js?

uri-js is an extensive library that allows you to parse, serialize, normalize, and resolve URIs, covering a wide range of functionalities required to handle different URI components. The library follows the RFC 3986 specification for URI and RFC 3987 for IRI (Internationalized Resource Identifiers).

Installation

  npm install uri-js

Parse URIs

Parse URIs into components using parse method to extract details such as scheme, host, path, query, and fragment:

  const URI = require('uri-js');
  const uri = 'https://example.com:8080/search?q=uri-js#section';

  const components = URI.parse(uri);
  console.log(components);
  // Output:
  // {
  //   scheme: 'https',
  //   host: 'example.com',
  //   port: 8080,
  //   path: '/search',
  //   query: 'q=uri-js',
  //   fragment: 'section'
  // }

Serialize URIs

Serialize URI components back into a URI string using the serialize method:

  const components = {
    scheme: 'https',
    host: 'example.com',
    port: 8080,
    path: '/search',
    query: 'q=uri-js',
    fragment: 'section'
  };

  const uri = URI.serialize(components);
  console.log(uri);
  // Output: 'https://example.com:8080/search?q=uri-js#section'

Normalize URIs

Normalize a URI to its canonical form using the normalize method:

  const uri = 'HTTPS://example.COM:80/search?q=uri-js#Section';

  const normalizedUri = URI.normalize(uri);
  console.log(normalizedUri);
  // Output: 'https://example.com:80/search?q=uri-js#section'

Resolve Relative URIs

Resolve a relative URI against a base URI using the resolve method:

  const baseURI = 'https://example.com:8080/root/';
  const relativeURI = '../../images/img.jpg';

  const resolvedURI = URI.resolve(baseURI, relativeURI);
  console.log(resolvedURI);
  // Output: 'https://example.com:8080/images/img.jpg'

Application Example: Building a URL Shortener

Here’s a brief example of how to use uri-js in a URL shortener application:

  const URI = require('uri-js');
  const urlStore = {};

  function shortenURL(longURL) {
    const components = URI.parse(longURL);
    const shortPath = components.host + components.path.replace(/\//g, '-');
    urlStore[shortPath] = longURL;
    return 'shrt.ly/' + shortPath;
  }

  function expandURL(shortURL) {
    const shortPath = shortURL.replace('shrt.ly/', '');
    return urlStore[shortPath];
  }

  // Usage
  const shortURL = shortenURL('https://example.com/this/is/a/long/path');
  console.log(shortURL);
  // Output: 'shrt.ly/example.com-this-is-a-long-path'

  const longURL = expandURL(shortURL);
  console.log(longURL);
  // Output: 'https://example.com/this/is/a/long/path'

In this example, we parse the long URL to generate a unique short path, store the mapping in a simple in-memory object, and later expand it back to the original URL.

Hash: 096754d9618eb9fc17844f8ea0d7a09c6fe3699479f343d72f0c29a373b5762a

Leave a Reply

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