URL Shortener

A URL shortener is a common full-stack machine-coding project.

Features

Backend routes

POST /api/urls
GET /:slug
GET /api/urls/:id/stats

MongoDB model

const urlSchema = new Schema({
  longUrl: { type: String, required: true },
  slug: { type: String, required: true, unique: true },
  clickCount: { type: Number, default: 0 },
  userId: { type: Schema.Types.ObjectId, ref: 'User' },
}, { timestamps: true })

Redirect flow

  1. User visits /:slug.
  2. Backend finds URL by slug.
  3. Backend increments click count.
  4. Backend redirects to long URL.
res.redirect(url.longUrl)

Edge cases

What this project proves