/how-to-build-lovable

How to build URL shortener app with Lovable?

Discover a step-by-step guide to build a URL shortener app using Lovable. Uncover best practices to craft an efficient, scalable solution.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to build URL shortener app with Lovable?

 
Creating a New Lovable Project
 

Using Lovable, start by creating a new project. In the Lovable dashboard, click “New Project” and choose Python as your language. Name your project (for example, “URL Shortener App”). This project will contain all of your code files.

 
Setting Up Dependencies
 

Because Lovable does not have a terminal, you install dependencies by creating a file that lists them. Create a new file in your project named requirements.txt and insert the following code snippet. This file tells Lovable which libraries your app needs.


Flask

Lovable will read this file and install Flask automatically.

 
Creating the Main Application File
 

Now create a new file named app.py. This file will contain all the logic for your URL shortener application. Open app.py in the Lovable code editor.

 
Adding URL Shortener Logic to Your App
 

Copy the following code snippet into your app.py file. This code uses Flask to create a simple web server that accepts a URL through a form, generates a random short code, and stores the URL in a temporary in-memory dictionary. When the short URL is visited, it redirects to the original URL.


from flask import Flask, request, redirect, render_template_string
import random
import string

app = Flask(name)

Dictionary to store the mapping between short codes and original URLs.

url_mapping = {}

Function to generate a random alphanumeric code.

def generate_short_code(length=6):
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

Route for the home page with a form to enter a URL.

@app.route('/', methods=['GET', 'POST'])
def home():
message = ''
if request.method == 'POST':
original_url = request.form.get('url')
short_code = generate_short_code()
url_mapping[short_code] = original_url
message = f"Short URL: {request.host_url}{short_code}"
return render_template_string("""


URL Shortener


URL Shortener






{{ message|safe }}




""", message=message)

Route to redirect short URL to the original URL.

@app.route('/<short_code>')
def redirect_short(short_code):
original_url = url_mapping.get(short_code)
if original_url:
return redirect(original_url)
return "URL not found", 404

Run the app on Lovable.

if name == 'main':
app.run(host='0.0.0.0', port=8000)

Save the app.py file after pasting the code.

 
Understanding the Code
 

The code is divided into several parts:

  • Imports and Initialization: Imports necessary modules and creates a Flask instance.
  • Data Storage: A simple dictionary url\_mapping is used to store the relationship between the short code and the original URL.
  • Short Code Generation: The function generate_short_code creates a random string of 6 characters.
  • The Home Route: Displays a form to input a URL. When submitted, it generates a short code and shows the short URL.
  • Redirection Route: When a short URL is accessed, the redirect_short function looks up the original URL from url_mapping and redirects the user.
  • Running the App: The standard Python block to run the Flask server.

 
Testing Your URL Shortener Application
 

In Lovable, click the “Run” button to start your application. The app will start on port 8000 and Lovable will provide a live URL for testing.

Visit the live URL in your web browser. You should see a form where you can enter a URL to shorten. After submitting a URL, a short URL will be displayed. Clicking the short URL should redirect you to your original URL.

 
Final Notes
 

Each time you make changes to your code, save the file and click “Run” in Lovable to update your live application. Remember that the URL mapping is stored in a temporary in-memory dictionary; if the server restarts, the data will be lost. For persistent storage, you would need to integrate a database.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

How to Build a URL Shortener App with Lovable Using Node.js, Express, and Mongoose


const express = require('express');
const mongoose = require('mongoose');
const crypto = require('crypto');

const app = express();
app.use(express.json());

mongoose.connect('mongodb://localhost:27017/lovable-shortener', {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

const urlSchema = new mongoose.Schema({
  originalUrl: { type: String, required: true },
  shortCode: { type: String, unique: true, required: true },
  createdAt: { type: Date, default: Date.now, expires: '30d' }
});

const Url = mongoose.model('Url', urlSchema);

function generateShortCode() {
  return crypto.randomBytes(4).toString('hex');
}

app.post('/api/shorten', async (req, res) => {
  const { originalUrl, customAlias } = req.body;
  let shortCode = customAlias ? customAlias : generateShortCode();
  try {
    const exists = await Url.findOne({ shortCode });
    if (exists) {
      return res.status(409).json({ error: 'Alias already exists' });
    }
    const newUrl = new Url({ originalUrl, shortCode });
    await newUrl.save();
    res.json({ shortUrl: `${req.protocol}://${req.get('host')}/${shortCode}` });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/:shortCode', async (req, res) => {
  try {
    const urlEntry = await Url.findOne({ shortCode: req.params.shortCode });
    if (!urlEntry) {
      return res.status(404).json({ error: 'URL not found' });
    }
    res.redirect(urlEntry.originalUrl);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => console.log('Server started on port 3000'));

How to Easily Build Your Own URL Shortener App with Lovable


const express = require('express');
const axios = require('axios');
const mongoose = require('mongoose');

const app = express();
app.use(express.json());

mongoose.connect('mongodb://localhost:27017/lovable-shortener', {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

const urlSchema = new mongoose.Schema({
  originalUrl: { type: String, required: true },
  shortCode: { type: String, unique: true, required: true },
  createdAt: { type: Date, default: Date.now }
});

const Url = mongoose.model('Url', urlSchema);

const ANALYTICS_API_URL = 'https://analytics.example.com/track';
const ANALYTICS_API_TOKEN = 'YOUR_ANALYTICS_TOKEN';

async function reportAnalytics(shortCode, req) {
  try {
    await axios.post(ANALYTICS_API_URL, {
      shortCode,
      ip: req.ip,
      userAgent: req.headers['user-agent'],
      timestamp: new Date()
    }, {
      headers: {
        'Authorization': `Bearer ${ANALYTICS_API_TOKEN}`
      }
    });
  } catch (error) {
    console.error('Analytics reporting failed:', error.message);
  }
}

app.get('/:shortCode', async (req, res) => {
  try {
    const urlData = await Url.findOne({ shortCode: req.params.shortCode });
    if (!urlData) {
      return res.status(404).json({ error: 'URL not found' });
    }
    reportAnalytics(req.params.shortCode, req);
    res.redirect(urlData.originalUrl);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

How to build a URL shortener using Express, PostgreSQL, Redis, and AMQP with Lovable?


const express = require('express');
const { Pool } = require('pg');
const Redis = require('ioredis');
const amqp = require('amqplib');

const app = express();
const pgPool = new Pool({
  connectionString: process.env.PG_CONNECTION_STRING
});
const redis = new Redis(process.env.REDIS\_URL);
let amqpChannel = null;

async function initAMQP() {
  try {
    const connection = await amqp.connect(process.env.AMQP\_URL);
    amqpChannel = await connection.createChannel();
    await amqpChannel.assertQueue('analytics', { durable: true });
  } catch (err) {
    console.error('Failed to initialize AMQP:', err.message);
  }
}
initAMQP();

app.get('/:shortCode', async (req, res) => {
  const { shortCode } = req.params;
  try {
    // Attempt to retrieve the original URL from Redis cache
    let originalUrl = await redis.get(`short:${shortCode}`);
    if (!originalUrl) {
      // Fallback to PostgreSQL if not in cache
      const result = await pgPool.query('SELECT original_url FROM urls WHERE short_code = $1', [shortCode]);
      if (result.rowCount === 0) {
        return res.status(404).json({ error: 'URL not found' });
      }
      originalUrl = result.rows[0].original\_url;
      // Cache the result for subsequent requests (expire in 1 hour)
      await redis.set(`short:${shortCode}`, originalUrl, 'EX', 3600);
    }
    // Asynchronously report analytics via AMQP
    if (amqpChannel) {
      amqpChannel.sendToQueue('analytics',
        Buffer.from(JSON.stringify({
          shortCode,
          ip: req.ip,
          userAgent: req.headers['user-agent'],
          timestamp: Date.now()
        })), { persistent: true }
      );
    }
    res.redirect(originalUrl);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => console.log('Server listening on port 3000'));

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation
Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Best Practices for Building a URL shortener app with AI Code Generators

 

Prerequisites

 

  • A basic computer with internet access.
  • An AI code generator tool (like GitHub Copilot, ChatGPT, etc.) to help automate parts of the coding process.
  • Familiarity with a simple programming language such as Python. Don’t worry if you’re not an expert—the AI tool will assist you.
  • A web browser and a text editor or an online IDE (like Replit) for writing your code.

 

Planning Your URL Shortener App

 

  • Decide the core functionality: converting a long URL into a shorter one, storing the mapping, and redirecting users.
  • Outline the features: simple input for users, shortening service, storage for URL mappings, and redirection to the original URL.
  • Decide on the technologies: for this guide, we will use Python with the Flask framework.

 

Setting Up Your Development Environment

 

  • Install Python on your computer if it is not already installed.
  • Set up an IDE or a text editor like Visual Studio Code or use an online environment like Replit.
  • Create a new project folder for your URL shortener app.
  • Inside your project folder, create a file named app.py which will contain your application code.

 

Generating Code with an AI Code Generator

 

  • Open your chosen code editor and start a new file (app.py).
  • Interact with your AI code generator by describing what you want; for example, “Create a Flask application that accepts a URL, generates a short code, stores the mapping, and then redirects when the short URL is accessed.”
  • Review the AI suggestions, modify them if necessary, and insert the code into app.py.

 

Implementing the URL Shortener Logic

 

  • Create the core functionality in your Flask application. Below is an example code snippet:
  • 
    from flask import Flask, request, redirect, jsonify
    import string
    import random
    
    

    app = Flask(name)

    Dictionary to hold URL mappings in memory

    url_mapping = {}

    def generate_short_code(length=6):
    characters = string.ascii_letters + string.digits
    return ''.join(random.choices(characters, k=length))

    @app.route('/shorten', methods=['POST'])
    def shorten_url():
    data = request.get_json()
    original_url = data.get('url')
    if not original_url:
    return jsonify({'error': 'URL is required'}), 400
    short_code = generate_short_code()
    url_mapping[short_code] = original_url
    short_url = request.host_url + short_code
    return jsonify({'short_url': short_url})

    @app.route('/<short_code>')
    def redirect_to_url(short_code):
    original_url = url_mapping.get(short_code)
    if original_url:
    return redirect(original_url)
    return jsonify({'error': 'Invalid short URL'}), 404

    if name == 'main':
    app.run(debug=True)



  • This code sets up a basic API endpoint that creates a short URL and a dynamic route to handle redirection.

 

Testing Your Application

 

  • Run your application using the command line by navigating to your project folder and typing python app.py (or use your online IDE’s run feature).
  • Use a tool like Postman or your browser to test the endpoints:
    • POST a JSON object with a long URL to the /shorten endpoint.
    • Copy the returned short URL and paste it into your browser to see if it redirects correctly.
  • Observe the terminal for any errors to adjust your code if needed.

 

Implementing Best Practices

 

  • Ensure proper error handling so that all user inputs are checked for validity. The example verifies if a URL is provided.
  • Use logging to capture important events such as errors or redirection attempts. This can be added as your application grows.
  • Consider storing URL mappings in a database for persistence instead of an in-memory dictionary.
  • Secure your application by validating input data and following best security practices.

 

Deploying Your URL Shortener App

 

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022