URL Shortener Service with NextJS Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will explore the process of building a URL shortener service using NextJS that takes a long URL and generates a shorter, more condensed version that redirects to the original URL when accessed. This shortened URL is typically much shorter and easier to share, especially in situations where character count is limited, such as in social media posts or text messages. Output Preview: Let us have a look at how the final output will look like. Prerequisites:NPM & NodeJSReactJSReactJS HooksNextJSApproach to Create URL Shortener Service:React component named Home manages URL shortening.Uses useState hook to manage state for original URL and shortened URL.shortURL function handles form submission, sending request to TinyURL API.If successful, sets state with shortened URL; else, shows error alert.Renders input field and submit button for entering URL.Displays shortened URL with anchor tag for opening in new tab.Uses security attributes target="_blank" rel="noopener noreferrer" for link.Steps to create the URL Shortener Service:Step 1: Create a application of NextJS using the following command. npx create-next-app@latest url-shortenerStep 2: Navigate to project directory cd url-shortenerProject Structure: The updated dependencies in package.json file will look like: "dependencies": { "react": "^18", "react-dom": "^18", "next": "14.1.3" }Example: Below are the files which describes the basic implementation of URL Shortener Service. CSS /* globals.css */ main { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; padding: 0 20px; text-align: center; } h1 { color: green; font-size: 2rem; margin-bottom: 20px; } form { display: flex; flex-direction: column; align-items: center; } input { width: 100%; padding: 10px; margin-bottom: 10px; } button { padding: 10px 20px; background-color: #007bff; color: #fff; border: none; cursor: pointer; } button:hover { background-color: #0056b3; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } JavaScript // page.js 'use client' import { useState } from "react"; import "./globals.css"; export default function Home() { const [url, setUrl] = useState(""); const [shortenedUrl, setShortenedUrl] = useState(""); async function shortURL(e) { e.preventDefault(); const response = await fetch(` https://tinyurl.com/api-create.php?url=${encodeURIComponent(url)}`); if (response.ok) { const data = await response.text(); setShortenedUrl(data); } else { alert("Error shortening URL"); } } return ( <main> <h1>GeeksForGeeks</h1> <h2>URL Shortener</h2> <form onSubmit={shortURL}> <input type="text" placeholder="Enter URL" value={url} onChange={(e) => setUrl(e.target.value)} /> <button type="submit">Shorten</button> </form> {shortenedUrl && ( <div> <p>Shortened URL:</p> <a href={shortenedUrl} target="_blank" rel="noopener noreferrer"> {shortenedUrl} </a> </div> )} </main> ); } Start your application using the below command. npm run devOutput: Naviage to the URL http://localhost:3000. Comment More infoAdvertise with us Next Article Contact Us Form using Next.js G ghuleyogesh Follow Improve Article Tags : Project Web Technologies ReactJS Dev Scripter Next.js Web Development Projects Dev Scripter 2024 Next.js - Projects +4 More Similar Reads Blogging Platform using Next JS In this project, we will explore the process of building The Blogging Platform with Next.js. Blogging Platform is a web application that allows users to create and publish blog posts. The platform provides a user-friendly interface for managing blog content and includes functionalities to create new 5 min read How to Create Todo App using Next.js ? In this article, we will create a to-do application and understand the basics of Next.js. This to-do list can add new tasks we can also delete the tasks by clicking on them.Next.js is a widely recognized React framework that eÂnables server-side rendering and enhanceÂs the developmeÂnt of interacti 4 min read Document Management System using NextJS The Document Management System is a web application developed using Next.js, that allows users to efficiently manage their documents. The system provides features for uploading, organizing, and retrieving documents. Users can upload documents through the web interface, which are then stored in local 5 min read Create a Quiz App with Next JS In this article, weâll explore the process of building a Quiz App utilizing NextJS. The quiz app provides users with a series of multiple-choice questions with options. Users can select the option and move to the next question. At the end, the user will be able to see the analysis of the quiz.Output 5 min read E-commerce Dashboard with NextJS This project is an E-commerce Dashboard built with Next.js, providing features such as a dynamic sidebar, responsive navigation, order analytics, and most sold items of the week. It shows various features such as product analytics, order management, and user interaction. Output Preview: Prerequisite 11 min read Social Networking Platform using Next.js The Social Networking Platform built with NextJS is a web application that provides users the functionality to add a post, like a post, and be able to comment on it. The power of NextJS, a popular React framework for building server-side rendered (SSR) and statically generated web applications, this 8 min read URL Shortener Service with NextJS In this article, we will explore the process of building a URL shortener service using NextJS that takes a long URL and generates a shorter, more condensed version that redirects to the original URL when accessed. This shortened URL is typically much shorter and easier to share, especially in situat 2 min read Contact Us Form using Next.js Creating a Contact Us form in Next.js involves setting up a form component, handling form submissions, and potentially integrating with a backend service or API to send the form data. In this article, we will create a Contact Us Form with NextJS.Output Preview: Letâs have a look at what our final pr 6 min read Blogging Platform using Next JS In this project, we will explore the process of building The Blogging Platform with Next.js. Blogging Platform is a web application that allows users to create and publish blog posts. The platform provides a user-friendly interface for managing blog content and includes functionalities to create new 5 min read Music Player App with Next.js and API In this tutorial, we'll create a Music Player App using NextJS, a React framework. Explore frontend development, API integration, and UI design to build a user-friendly app for seamless music enjoyment. Join us in harmonizing code and creativity to craft an engaging Music Player App with NextJS in t 3 min read Like