Node’s URL module

As part of Code School, we get to periodically give tiny little lighting talks—just 3–5 minute reviews of a tiny little piece of what we’re learning about. We’re doing this so that people can get comfortable giving presentations and speaking about tech things. For me, that’s not really an issue; my business partner Amanda and I have done a number of talks on ebooks, both solo and co-presenting (including one 4 hour overview of the then-new epub 3.0 spec at Tools of Change 2012); the only challenge to a 3–5 minute talk was saying anything useful in that short a time. Fortunately, the topic I got to go over is pretty short and sweet: The URL module in Node.

As you might expect, the URL module is all about dealing with URLs. You can go from URL to object, object to URL, and also resolve them, which is (to my understanding; the documentation wasn’t as clear as it could have been) basically taking, say, a web address and a file path and merging them together. The only really complex part of the url module is figuring out which parts of the URL correspond to which object properties. In order to make that easy to see, I made this handy diagram:

diagram of the parts of a URL

You can see that the whole string is the href, which is then divided (and subdivided, and sub-subdivided) into other chunks. I’ve mostly used colors and fonts to group bits together, with black text as a sort of wildcard representing text of any color; ‘path’ in black Courier indicates all the bits of the URL above it that are in Courier, regardless of the color, whereas the green Courier ‘pathname’ only indicates the bit of the URL in green Courier.

The talk was fine, and the diagram may be useful to me next time I need to play around with URLs in node. Who knows, maybe someone else will find it useful as well.

Leave a Reply

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