Create a TinyURL using Node
Updated onbyAlan Morel
Table of Contents
When you want to take long link and shorten it, you can utilize a service called TinyURL.
TinyURL is a service that lets you create shorter URLs when given a longer one.
For example, it can turn:
BASHhttps://sabe.io/blog/node-create-tiny-url
into this:
BASHhttps://tinyurl.com/22dgx56k
In this post, we'll learn how we can create TinyURLs in Node.
Creating TinyURLs in Node
Thankfully, TinyURL provides us an API that we can use.
Let's create a function that uses TinyURL to create a TinyURL.
JAVASCRIPTconst getTinyURL = async (url) => {
const response = await fetch(`https://tinyurl.com/api-create.php?url=${url}`);
return response.text();
};
It's that simple, we just make a request to TinyURL and get the response in plain text.
Let's try it out:
JAVASCRIPTconst getTinyURL = async (url) => {
const response = await fetch(`https://tinyurl.com/api-create.php?url=${url}`);
return response.text();
};
const url = "https://sabe.io/blog/node-create-tiny-url";
const tinyURL = await getTinyURL(url);
console.log(tinyURL);
BASHhttps://tinyurl.com/22dgx56k
Now when you click on the link, you'll be redirected to the original URL:
BASHhttps://sabe.io/blog/node-create-tiny-url
Conclusion
In this post, we looked at how to create TinyURLs in Node.
Just use the TinyURL endpoint, pass in your URL, and get a shortened version back.
Thanks for reading!
Leave us a message!
×
- Getting Started with Solid
- Best Visual Studio Code Extensions for 2022
- How to build a Discord bot using TypeScript
- Getting Started with Deno
- Learn how to use v-model with a custom Vue component
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Learn how to build a Slack Bot using Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with React
- Getting Started with Vuex: Managing State in Vue
- Using Axios to Pull Data from a REST API