Day 90

Project: Full-Stack App

45 min
JavaScript 100 Days

90 din ka final backend project — ek complete URL shortener service banate hain jisme user registration, JWT auth, URL shortening, analytics, aur production deployment hogi.

URL Shortener Architecture

Complete project structure.

url-shortener.js
javascript
// URL Shortener — complete features:
// - User registration & JWT login
// - URL shorten karo (custom alias support)
// - Redirect short → original URL
// - Click analytics (count, last accessed)
// - User dashboard (their URLs)

// URL model
const urlSchema = new mongoose.Schema({
  originalUrl: { type: String, required: true },
  shortCode: { 
    type: String, 
    unique: true,
    default: () => Math.random().toString(36).substring(2, 8)
  },
  customAlias: { type: String, unique: true, sparse: true },
  owner: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  clicks: { type: Number, default: 0 },
  lastAccessed: Date,
  expiresAt: Date,
  isActive: { type: Boolean, default: true }
}, { timestamps: true });

// Short URL redirect
app.get("/:code", async (req, res) => {
  const url = await Url.findOneAndUpdate(
    { 
      $or: [{ shortCode: req.params.code }, { customAlias: req.params.code }],
      isActive: true
    },
    { $inc: { clicks: 1 }, lastAccessed: new Date() },
    { new: true }
  );
  
  if (!url) return res.status(404).send("URL not found");
  if (url.expiresAt && url.expiresAt < new Date()) {
    return res.status(410).send("URL expired");
  }
  
  res.redirect(url.originalUrl);
});

🎯 Practice Challenge

URL shortener deploy karo — Railway ya Render par. Custom domain add karo, aur Postman documentation banao.