DZone
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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Mastering Node.js: The Ultimate Guide
  • Node.js: Architectural Gems and Best Practices for Developers to Excel
  • Using Render Log Streams to Log to Papertrail
  • How To Use the Node Docker Official Image

Trending

  • Revolutionizing Software Testing
  • How to Submit a Post to DZone
  • Designing Databases for Distributed Systems
  • Agile Estimation: Techniques and Tips for Success
  1. DZone
  2. Coding
  3. JavaScript
  4. Node.js: Expanding Shortened URLs

Node.js: Expanding Shortened URLs

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Apr. 17, 12 · Interview
Like (0)
Save
Tweet
Share
5.19K 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.

Related

  • Mastering Node.js: The Ultimate Guide
  • Node.js: Architectural Gems and Best Practices for Developers to Excel
  • Using Render Log Streams to Log to Papertrail
  • How To Use the Node Docker Official Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: