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
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    Telephone Link Protocol

    We've always been able to create links with protocols other than the usual HTTP, like mailto, skype, irc ,and more;  they're an excellent convenience to visitors.  With mobile phone browsers having become infinitely more usable, we can now extend that convenience to phone numbers: The tel...

  • By
    Xbox Live Gamer API

    My sharpshooter status aside, I've always been surprised upset that Microsoft has never provided an API for the vast amount of information about users, the games they play, and statistics within the games. Namely, I'd like to publicly shame every n00b I've baptized with my...

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!