A fast, lightweight URL shortener API built with Node.js, Express, and MongoDB. Generates short IDs, handles redirects, and tracks visit analytics.
https://snapurl-hf8u.onrender.com
- Runtime — Node.js
- Framework — Express.js
- Database — MongoDB (via Mongoose)
- Hosting — Render
- Short ID generation — nanoid / shortid
Shorten a long URL.
Request body:
{
"url": "https://example.com"
}Response:
{
"shortID": "kPDoCpnR",
"link": "https://snapurl-hf8u.onrender.com/api/kPDoCpnR"
}Redirects to the original URL and records a visit timestamp.
GET https://snapurl-hf8u.onrender.com/api/kPDoCpnR
→ 302 Redirect to https://example.com
Returns visit count and full visit history for a short URL.
Response:
{
"totalVisits": 3,
"analytics": [
{ "timestamp": 1776341689084, "_id": "..." },
{ "timestamp": 1776341712000, "_id": "..." }
]
}- Node.js v18+
- MongoDB Atlas account (or local MongoDB)
git clone https://github.com/yourusername/snapurl-backend.git
cd snapurl-backend
npm installCreate a .env file in the root:
MONGO_URI=your_mongodb_connection_string
BASE_URL=http://localhost:8000
PORT=8000node server.jsThe API will be available at http://localhost:8000.
snapurl-backend/
├── controller/
│ └── url.controller.js # generateShortUrl, getShortUrl, getAnalytics
├── routes/
│ └── url.router.js # Route definitions
├── models/
│ └── url.model.js # Mongoose schema
├── app.js # Express app setup (CORS, middleware, routes)
└── server.js # Server entry point
- Push your code to GitHub
- Go to render.com → New Web Service → Connect your repo
- Set the following environment variables in the Render dashboard:
| Key | Value |
|---|---|
MONGO_URI |
Your MongoDB Atlas connection string |
BASE_URL |
https://your-app.onrender.com |
PORT |
8000 |
Render redeploys automatically on every push to main.
- The router must define
/analytics/:shortIDbefore/:shortID— otherwise Express matches analytics requests as short IDs. BASE_URLmust be set in the Render environment dashboard, not just in the local.envfile (.envis gitignored and never pushed).