Project Overview
URL Shortener
Build a service that creates short URLs and tracks analytics.
Features
- Create short URLs from long URLs
- Custom short codes (optional)
- Redirect with analytics tracking
- Click tracking (IP, timestamp, referrer)
- User accounts and URL management
- URL expiration
- Rate limiting
Progressive Enhancements
- V1: Basic shortening + redirect
- V2: Analytics + user accounts
- V3: Redis caching + Docker
- V4: Rate limiting + custom codes
Architecture
Client → Express API
↓ checks Redis cache
↓ cache miss → MongoDB
↓ generates short code
↓ stores mapping
redirect to original URL
Architecture — What Goes Where and Why
Project Architecture
Before writing any code, let's understand what files we need and where they go. Think of this like a blueprint for a house — you don't start building without a plan.
The File Tree
Every project needs a folder structure. Here is ours:
├── server.js
├── database.js
├── routes/urls.js
├── public/index.html
├── public/js/app.js
What Each File Does
Let me explain each file like you're 5 years old:
server.js — Express server
Handles URL shortening requests and redirects users to original URLs.
database.js — URL storage
Maps short codes to original URLs in SQLite with click tracking.
routes/urls.js — URL API routes
POST /shorten creates links, GET /:code redirects, GET /stats shows analytics.
public/index.html — Shortener interface
Input field, shorten button, result display with copy button.
public/js/app.js — Frontend logic
Submits URLs to API, displays shortened links, copies to clipboard.
Why This Structure?
You might wonder: why use folders at all? Why not put everything in one place?
Because as projects grow, you might have:
- Multiple CSS files
- Many JavaScript files
- Images, fonts, and other assets
If they're all in one folder, it's chaos. Folders keep things organized.
// BAD — everything in one folder
index.html
styles.css
main.css
dark.css
app.js
utils.js
helper.js
// GOOD — organized in folders
index.html
css/
styles.css
main.css
js/
app.js
utils.js
The Golden Rule
When you start a project, ALWAYS:
- Create the folder structure FIRST
- Then fill in the files one by one
- Never write everything in one giant file
This is how professional developers work. Always.
Step 1 — Project Setup (Creating the Empty House)
Setting Up the Project
We are going to create an empty project from scratch. This is the exact process professional developers use every single day.
Open Your Terminal
Your terminal (also called "command line" or "console") is where you type commands to talk to your computer.
- Mac: Open "Terminal" app
- Windows: Open "PowerShell" or "Command Prompt"
- VS Code: Press Ctrl+` (backtick) to open the built-in terminal
Step-by-Step Commands
Type each of these commands, one at a time, pressing Enter after each:
mkdir url-shortener && cd url-shortener && git init && npm init -y && npm install express nanoid better-sqlite3 && mkdir routes public public/js
What Just Happened?
Let me explain each command like you're 5:
mkdir [folder] — "Make a new box." This creates a folder on your computer.
cd [folder] — "Go inside the box." Now when you type commands, they happen inside this folder.
git init — "Start keeping a diary." Git will now track every change you make. If you break something, you can go back in time.
npm init -y — "Create an ID card for this project." It creates package.json with default settings. The -y means "yes to all questions."
touch [file] — "Create an empty file." Think of it as a blank piece of paper.
Verify It Worked
Type this command to see your files:
ls -la
The .gitignore File
Open .gitignore and add this:
node_modules/
.DS_Store
*.log
.env
Why? Because node_modules can be HUGE — thousands of files. We don't want to save all of them in Git. We can always recreate them with npm install.
Your First Git Commit
Now save everything to Git:
git add .
git commit -m "Initial project setup"
Congratulations! You just set up a project like a real developer.
Step 2 — Building the Core (Step by Step)
Building the Application
Now let's build the application step by step. Follow each step in order.
Build Steps
- Create database.js with urls table (code, original_url, clicks, created_at)
- Create server.js with Express middleware setup
- Create routes/urls.js with POST /shorten (generate nanoid code)
- Add GET /:code redirect endpoint (increment click count)
- Add GET /api/stats/:code for click analytics
- Build index.html with input and result display
- Write app.js to call API and display shortened URL
- Add copy-to-clipboard functionality with feedback
- Add click tracking and stats display
- Add URL validation (must start with http/https)
- Add rate limiting to prevent abuse
- Test: shorten URL → redirect → check stats
- Commit and deploy
How Each Step Works
Step 1 — Create the entry point
Every application starts with an entry file. This is the file that runs first when someone opens your app.
Think of it like the front door of a house. Everything starts when you walk through it.
Step 2 — Set up the structure
Before writing features, we set up the basic layout. This is like putting up the walls before painting them.
Step 3 — Add the main functionality
Now we add what makes the app actually DO something. This is the "brain" of the application.
Step 4 — Connect everything
We wire the pieces together. The HTML connects to CSS. The JavaScript connects to HTML. Everything talks to each other.
Step 5 — Test and fix
We try everything, find bugs, and fix them. Real developers spend 30% of their time testing.
Save and Test
After each step, save your files and refresh the browser. If something breaks, check the browser console (F12 → Console tab).
Commit after each major step:
git add .
git commit -m "Describe what you just built"
Pro Tip
Build in small steps. Don't write 100 lines and then test. Write 10 lines, test, fix, then write 10 more. This catches errors early when they're easy to fix.
Core Implementation
URL Shortening Logic
Short Code Generation
import { nanoid } from 'nanoid';
import base62 from 'base62';
// Option 1: nanoid (recommended)
const shortCode = nanoid(7); // "V1StGXR8_Z5jdHi6B-myT"
// Option 2: base62 encoding
function generateShortCode(id) {
return base62.encode(id);
}
// Option 3: Hash-based
import crypto from 'crypto';
function hashUrl(url) {
return crypto.createHash('sha256').update(url).digest('base64url').slice(0, 7);
}
URL Model
const urlSchema = new mongoose.Schema({
originalUrl: { type: String, required: true },
shortCode: { type: String, unique: true, index: true },
user: { type: ObjectId, ref: 'User' },
clicks: { type: Number, default: 0 },
expiresAt: Date,
customCode: { type: String, unique: true, sparse: true }
}, { timestamps: true });
Redirect with Analytics
router.get('/:code', async (req, res) => {
const { code } = req.params;
// Try cache first
let url = await redis.get(`url:${code}`);
if (url) {
url = JSON.parse(url);
} else {
url = await Url.findOne({ $or: [{ shortCode: code }, { customCode: code }] });
if (!url) return res.status(404).json({ error: 'URL not found' });
await redis.setex(`url:${code}`, 3600, JSON.stringify(url));
}
// Track click asynchronously
trackClick(url._id, req);
// Update click count
await Url.findByIdAndUpdate(url._id, { $inc: { clicks: 1 } });
res.redirect(301, url.originalUrl);
});
async function trackClick(urlId, req) {
await Click.create({
url: urlId,
ip: req.ip,
userAgent: req.headers['user-agent'],
referrer: req.headers.referer,
timestamp: new Date()
});
}
Analytics Endpoint
router.get('/api/urls/:id/analytics', auth, async (req, res) => {
const analytics = await Click.aggregate([
{ $match: { url: new mongoose.Types.ObjectId(req.params.id) } },
{
$group: {
_id: {
date: { $dateToString: { format: '%Y-%m-%d', date: '$timestamp' } }
},
clicks: { $sum: 1 }
}
},
{ $sort: { '_id.date': 1 } }
]);
res.json(analytics);
});
Redis Caching
Adding Redis
Why Redis for URL Shortener?
- Fast lookups — O(1) access vs MongoDB query
- High throughput — millions of reads per second
- TTL support — URLs can expire automatically
Redis Setup
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);
// Cache URL mapping
async function cacheUrl(code, urlData) {
await redis.setex(
`url:${code}`,
3600, // 1 hour TTL
JSON.stringify(urlData)
);
}
// Get cached URL
async function getCachedUrl(code) {
const cached = await redis.get(`url:${code}`);
return cached ? JSON.parse(cached) : null;
}
// Invalidate cache on update
async function invalidateUrl(code) {
await redis.del(`url:${code}`);
}
Cache Strategy
Request → Check Redis → Hit? Return cached
→ Miss? Query MongoDB → Cache result → Return
Rate Limiting with Redis
async function rateLimit(ip, limit = 100, window = 60) {
const key = `ratelimit:${ip}`;
const current = await redis.incr(key);
if (current === 1) await redis.expire(key, window);
return current <= limit;
}
Docker Compose
version: '3.8'
services:
app:
build: .
ports:
- "5000:5000"
environment:
- MONGODB_URI=mongodb://mongo:27017/urlshortener
- REDIS_URL=redis://redis:6379
depends_on:
- mongo
- redis
mongo:
image: mongo:6
volumes:
- mongo-data:/data/db
redis:
image: redis:7-alpine
volumes:
- redis-data:/data
volumes:
mongo-data:
redis-data:
Common Mistakes (What Goes Wrong)
Mistakes Everyone Makes
Every developer makes these mistakes. Knowing them ahead of time saves hours of debugging.
Mistake 1: Forgetting to Connect Files
<!-- WRONG — CSS is not linked, page looks ugly -->
<link rel="stylesheet" href="style.css">
<!-- RIGHT — file path matches actual location -->
<link rel="stylesheet" href="css/styles.css">
How to check: Right-click the page → Inspect → Network tab → look for red (failed) resources.
Mistake 2: JavaScript Before HTML
<!-- WRONG — JavaScript runs before HTML exists -->
<script src="app.js"></script>
<body>...</body>
<!-- RIGHT — JavaScript runs after HTML loads -->
<body>...</body>
<script src="app.js"></script>
Mistake 3: Not Using preventDefault()
// WRONG — page reloads when you submit
form.addEventListener('submit', () => {
// This code runs, but then the page reloads!
});
// RIGHT — preventDefault stops the reload
form.addEventListener('submit', (e) => {
e.preventDefault(); // Now the page doesn't reload
});
Mistake 4: Modifying State Without Re-rendering
// WRONG — changes data but screen stays the same
todos.push(newTodo);
// RIGHT — always update the screen after changing data
todos.push(newTodo);
render(); // Now the screen shows the new todo
Mistake 5: Not Handling Empty Input
// WRONG — adds empty tasks
addTodo(""); // Adds a blank todo
// RIGHT — check for empty input
if (text.trim()) {
addTodo(text);
}
Mistake 6: Ignoring Errors
How to check for errors:
- Press F12 in your browser
- Click "Console" tab
- Look for red text
- Click on it to see what went wrong
If you see red text, DON'T IGNORE IT. That's the computer telling you something is broken.
Debugging Checklist
When something doesn't work:
- Check the console — F12 → Console (red text = error)
- Check the Network — F12 → Network (red lines = failed requests)
- Check file paths — Are CSS/JS files in the right folder?
- Check spelling — Typos in IDs, class names, function names
- Check the basics — Is the server running? Is the URL correct?
The 5-Minute Rule
If you've been stuck for 5 minutes:
- Stop
- Re-read the error message
- Search the error on Google/Stack Overflow
- If still stuck, ask for help
Don't waste hours on something that might be a simple typo.
Testing Everything Works
Testing Your App
A real developer NEVER assumes code works. They TEST it.
Why Test?
- Users will find every bug you missed
- Bugs found later cost 10x more to fix
- Testing gives you confidence to make changes
Manual Testing Checklist
Go through this checklist for EVERY feature:
□ Page loads without errors (check browser console — F12)
□ All buttons work when clicked
□ All forms submit correctly
□ Input validation works (empty, special characters)
□ Data saves and loads correctly
□ Responsive on mobile (resize browser)
□ No console errors
□ All links work
□ Loading states appear during API calls
□ Error messages show when things fail
How to Check for Errors
Press F12 in your browser. Click the "Console" tab.
- Empty = Good! No errors.
- Red text = Bad. Click it to see what went wrong.
Edge Cases to Test
Real users do weird things. Test these:
- Empty input — What happens if you submit nothing?
- Very long input — Paste a 1000-character string
- Special characters — Type < > & " ' / 4. Rapid clicking — Click a button 10 times fast
- No internet — Turn off WiFi and try
- Refresh mid-action — Reload while something is happening
Browser Testing Tools
| Tool | How to Open | What It Shows |
|---|---|---|
| Console | F12 → Console | JavaScript errors and logs |
| Network | F12 → Network | API requests and responses |
| Elements | F12 → Elements | Live HTML and CSS |
| Device Mode | Ctrl+Shift+M | Mobile screen simulation |
Git Commit
After testing, commit your work:
git add .
git commit -m "Complete [project name] with testing"
What You Just Learned
Summary — Skills You Now Have
Technical Skills
- Project Structure — You know how to organize files in folders
- Git — You can initialize, add, commit, and push
- Version Control — You can track changes and go back in time
- HTML/CSS/JavaScript — You can build interactive web pages
- DOM Manipulation — You can change what's on screen with code
- Event Handling — You can respond to clicks, inputs, and other actions
- State Management — You can track and update application data
- Debugging — You can find and fix errors using browser tools
- Testing — You can verify your app works correctly
Thinking Skills
- Architecture — You planned the project BEFORE writing code
- Problem Solving — You broke a big problem into small steps
- Debugging — You systematically found and fixed errors
- Documentation — You wrote notes for future you (and other developers)
The Pattern
Every frontend application follows this pattern:
State (data) → Render (draw) → Event (respond) → Update State → Render again
React, Vue, Angular, Svelte — they ALL do this. You just learned the fundamental pattern that powers the entire web.
What's Next?
Look at the next project in the roadmap. Each new project builds on what you learned here. The skills compound — every project makes you a better developer.
Remember: every expert was once a beginner who didn't give up.
Quick Reference
Quick Reference
File Overview
- server.js: Express server
- database.js: URL storage
- routes/urls.js: URL API routes
- public/index.html: Shortener interface
- public/js/app.js: Frontend logic
Setup Commands
mkdir url-shortener && cd url-shortener && git init && npm init -y && npm install express nanoid better-sqlite3 && mkdir routes public public/js
Common Patterns
// Select an element
const el = document.getElementById('myId');
// Add event listener
el.addEventListener('click', (e) => {
e.preventDefault();
});
// Update the DOM
el.innerHTML = '<p>New content</p>';
el.textContent = 'Text only';
Debugging
F12 → Console → Check for errors
F12 → Network → Check API requests
F12 → Elements → Inspect HTML/CSS
Ctrl+Shift+M → Mobile view
Git Commands
git init # Start a repo
git add . # Stage all changes
git commit -m "message" # Save changes
git push # Upload to GitHub
Quiz
1. Why use Redis instead of just MongoDB for URL lookups?
2. What is the benefit of short codes being 7 characters?
Flashcards
Question
What is cache-aside pattern?
Click to reveal answer
Answer
Application checks cache first; on miss, queries database and populates cache for next request
Question
What is rate limiting?
Click to reveal answer
Answer
Restricting the number of requests a client can make in a given time window to prevent abuse
Revision Notes
Key Takeaways
- 1.Short codes need enough entropy to avoid collisions
- 2.Redis caching dramatically improves read performance
- 3.Analytics tracking should be asynchronous
- 4.Rate limiting prevents abuse
- 5.Docker Compose simplifies multi-service setup
Interview Tips
- •Design the URL shortening algorithm
- •Explain Redis caching strategy
- •Discuss how to handle high-traffic scenarios
Cheat Sheet
URL Shortener
Short Code
import { nanoid } from 'nanoid';
const code = nanoid(7);
Redis Cache
await redis.setex(`url:${code}`, 3600, JSON.stringify(data));
const cached = await redis.get(`url:${code}`);
Rate Limit
const current = await redis.incr(key);
if (current === 1) await redis.expire(key, window);
return current <= limit;