Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including url2 with all npm packages installed. Try it out:

var url2 = require("url2")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

url2 v2.0.0

Node's URL module plus relative pathing


This module builds upon the existing URL module in NodeJS, but adds relative(source, target) which returns the shortest relative path between any two equally qualified URL’s. If the paths are not equally qualified, it returns the target.

In addition, this package augments the URL object definition as returned by parse and consumed by format.

  • pathname is broken down into
    • root: whether the path is qualified from the root of the domain.
    • relative: the relative path, from the root if pathname is qualified from the domain root.
      • directories: an array of path components
      • file: the name of the file, or null if the URL points to a directory, indicated by a final slash.

Additionally, format uses the path property if it exists, instead of pathname and search.

Based on my earlier work for Narwhal.

Copyright 2012 Kristopher Michael Kowal. All rights reserved. MIT License

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free