DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Node.js: Expanding Shortened URLs

Node.js: Expanding Shortened URLs

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Apr. 17, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
4.86K Views

Join the DZone community and get the full member experience.

Join For Free
This blog post explains how one can use Node.js to expand a URL that has been shortened by a service such as t.co (built into Twitter) and bit.ly. We’ll look at a simple implementation and at an advanced implementation that uses promises.

 

The minimum

You need to install Mikeal Rogers’ request module:

npm install request

That module automatically follows all redirects from the shortened URL. Once you are at your final destination, you only need to find out where you are:

var request = require("request");

function expandUrl(shortUrl) {
    request( { method: "HEAD", url: shortUrl, followAllRedirects: true },
        function (error, response) {
            console.log(response.request.href);
        });
}

Prettier with promises

If you want to write a function that returns the expanded URL, more work is needed. You have the option of using a callback, but promises usually lead to prettier code. Let’s use Kris Kowal’s Q module:

npm install q

The “promising” code looks as follows.

var Q = require("q");
var request = require("request");

function expandUrl(shortUrl) {
    var deferred = Q.defer();
    request( { method: "HEAD", url: shortUrl, followAllRedirects: true },
        function (error, response) {
            if (error) {
                deferred.reject(new Error(error));
            } else {
                deferred.resolve(response.request.href);
            }
        });
    return deferred.promise;
}

Invoking the function works like this: 

expandUrl("http://example.com")
.then(function (longUrl) {
    console.log(longUrl);
});

 

Node.js

Published at DZone with permission of Axel Rauschmayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is ERP Testing? - A Brief Guide
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java
  • The Engineer’s Guide to Creating a Technical Debt Proposal
  • Migrating From Heroku To Render

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo