Part of series: System Design Roadmap
Week 1 Day 5: Mini Project - URL Shortener API
Welcome to our first Mini Project! We will build a simple version of Bit.ly.
Requirements
- Shorten: Accept a long URL, return a short ID.
- Redirect: Access short ID, redirect to long URL.
- Data: Store in memory (for now).
The Stack
- Language: JavaScript (Node.js)
- Framework: Express.js
- Database: In-Memory Map (Future: Redis/Postgres)
Implementation
1. Setup
npm init -y
npm install express shortid
2. The Code (server.js)
const express = require('express');
const shortid = require('shortid');
const app = express();
app.use(express.json());
// Database (In-Memory)
const urlDatabase = {};
// 1. Shorten URL
app.post('/shorten', (req, res) => {
const { longUrl } = req.body;
if (!longUrl) return res.status(400).json({ error: 'longUrl required' });
const id = shortid.generate();
urlDatabase[id] = longUrl;
res.json({
original: longUrl,
short: `http://localhost:3000/${id}`
});
});
// 2. Redirect
app.get('/:id', (req, res) => {
const { id } = req.params;
const longUrl = urlDatabase[id];
if (longUrl) {
res.redirect(longUrl);
} else {
res.status(404).send('URL not found');
}
});
app.listen(3000, () => console.log('Server running on port 3000'));
Testing
- POST to
http://localhost:3000/shortenBody:{ "longUrl": "https://www.google.com" }Response:{ "short": "http://localhost:3000/ad23s" } - GET
http://localhost:3000/ad23sResult: Redirects to Google!
System Design Critique
This works for 1 user. What if we have 1 million?
- Issue: In-memory data is lost on restart.
- Fix: Use a Database (Week 3).
- Issue: Single server canโt handle load.
- Fix: Multiple servers + Load Balancer (Week 2).
- Issue: Short IDs might collide (rare, but possible).
Next week, we tackle Scalability! We will make this robust. ๐