Follow URL Redirects with Node.js

By  on  

URL shorteners are a dime a dozen these days, and it is quite nice to have a pretty URL instead of a mile long string, but there are some downsides to URL shorteners:  they can mask dangerous URLs and getting to the endpoint can be slow, since you end up making multiple requests.  And what if a shortener sold out to a porn company?!  Whoa!

A while back I wrote a post about following URLs from command line with cURL.  Since I love JavaScript and Node.js is in full flight, I want to show you linkfollower, a Node.js utility for following URL redirects and getting the final landing URL.

Start by installing linkfollower:

yarn add linkfollower
# or `npm install linkfollower`

With linkfollower installed globally, we can use the follow command to follow the series of redirects until the final URL:

# follow {url}
follow http://davidwalsh.name/css

# RESULT:
# http://davidwalsh.name/css -> 301
# https://davidwalsh.name/css -> 301
# https://davidwalsh.name/css-animation-callback -> 200

URL shorteners can be likened to a blindfold -- the promise of going one place but possible end up in another.  Using linkfollower is a good practice if you care to be secure with links.

Recent Features

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    Add Site Screenshots for External Links Using MooTools Tooltips

    Before you send your user to an unknown external website, why not provide them a screenshot of the site via a tooltip so they may preview the upcoming page? Here's how you can do just that using MooTools. The MooTools JavaScript The first step is to grab...

  • By
    Google Font API

    Google recently debuted a new web service called the Font API.  Google's Font API provides developers a means by which they may quickly and painlessly add custom fonts to their website.  Let's take a quick look at the ways by which the Google Font...

Discussion

  1. The request module (https://github.com/request/request) also does this very nicely with followRedirect parameter (maxRedirects is also another handy parameter to deal with loops).

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!