A RESTful URL shortening service built with Express and MongoDB (Mongoose).
src/
config/ Environment loading + validation, DB connection
models/ Mongoose schemas
services/ Business logic (talks to models, throws ApiError)
controllers/ Thin HTTP layer (parses req, calls service, shapes res)
routes/ Express routers, wired to controllers + middleware
middlewares/ Validation, request logging, 404, centralized error handler
utils/ ApiError, ApiResponse, asyncHandler, logger, short code generator
app.js Express app assembly (no listening)
server.js Entry point: connects DB, then starts the HTTP server
app.js and server.js are split on purpose: app.js can be imported by
integration tests (supertest, etc.) without opening a real port or DB
connection.
npm install
cp .env.example .env # then fill in MONGO_DB_CONN_STRING
npm run dev # nodemon, for local development
npm start # plain node, for productionBase path: /api/v1/urls
| Method | Path | Description |
|---|---|---|
| POST | /api/v1/urls |
Create a short URL. Body: { "url": "https://..." } |
| GET | /api/v1/urls/:shortCode |
Get metadata for a short code |
| PUT | /api/v1/urls/:shortCode |
Update the long URL behind a code |
| DELETE | /api/v1/urls/:shortCode |
Delete a short URL |
| GET | /:shortCode |
Redirect (302) to the long URL, increments accessCount |
All responses use a consistent envelope:
{ "success": true, "message": "Short URL created", "data": { ... } }Errors:
{ "success": false, "message": "No URL found for short code 'abc123'" }