This project implements a URL shortening service built with Node.js, Express, and MongoDB. It features both a clean web interface and API endpoints to create shortened URLs, redirect to original URLs, and view analytics for each shortened URL. Additionally, it includes user authentication for secure access.
- Node.js: JavaScript runtime for building server-side applications
- Express.js: Web framework for creating robust APIs
- MongoDB: NoSQL database for storing URL and user data
- Mongoose: ODM library for MongoDB and Node.js
- Nanoid: Library for generating unique short IDs
- UUID: Library for generating session IDs
- EJS: Templating engine for rendering dynamic HTML pages
- CSS: Custom styling with responsive design
- JWT (JSON Web Tokens): Stateless authentication mechanism
- MVC Architecture: Model-View-Controller design pattern
- Cookie-Parser: Middleware for parsing cookies in requests
shorten-url/
├── controllers/
│ ├── urlController.js # Controllers for handling URL shortening requests
│ └── userControllers.js # Controllers for user authentication
├── middleware/
│ └── auth.js # Middleware for user authentication and session restriction
├── models/
│ ├── url.js # Mongoose schema and model for URLs
│ └── user.js # Mongoose schema and model for users
├── public/
│ ├── styles.css # CSS styling for the home page
│ ├── styles-login.css # CSS styling for the login page
│ └── styles-signup.css # CSS styling for the signup page
├── routes/
│ ├── urlRoute.js # Express routes for URL endpoints
│ └── userRoutes.js # Express routes for user authentication
├── service/
│ └── auth.js # Service for managing user sessions
├── view/
│ ├── home.ejs # EJS template for the home page
│ ├── login.ejs # EJS template for the login page
│ └── sign-up.ejs # EJS template for the signup page
├── connection.js # MongoDB connection setup
├── index.js # Main application entry point with middleware setup
└── README.md # Project documentation
The application provides a user-friendly web interface where users can:
- Enter a URL to be shortened
- View and copy the shortened URL
- Access the original URL by clicking the shortened link
- Sign up and log in to manage their sessions
The interface is fully responsive and styled with a modern, clean design.
| Method | Endpoint | Description |
|---|---|---|
| GET | /url | Render the home page |
| POST | /url | Create a shortened URL |
| GET | /url/:shortId | Redirect to the original URL |
| GET | /url/analytics/:shortId | Get click analytics for a short URL |
| GET | /user/login | Render the login page |
| POST | /user/login | Log in a user and return a JWT token |
| GET | /user/signup | Render the signup page |
| POST | /user/signup | Register a new user |
shortId(String, required): Unique short identifierredirectUrl(String, required): Original URL to redirect tovisitHistory(Array): Collection of timestamp objects for each visittimestamps: Automatically track creation and update times
username(String, required): Unique usernameemail(String, required): User's email addresspassword(String, required): User's passwordtimestamps: Automatically track creation and update times
-
Clone the repository
git clone <repository-url> cd shorten-url
-
Install dependencies
npm install
-
Start MongoDB
# Make sure MongoDB is running on your system -
Start the server
npm start
-
Access the application
The web interface will be available at http://localhost:3000/url
- Clean, responsive design
- Form validation for URL input
- Visual feedback for successful URL shortening
- Animated UI elements for better user experience
- Stateless authentication using JSON Web Tokens (JWT)
- Secure signup and login functionality
- Middleware to restrict access to logged-in users for URL-related routes
- Role-based access control for specific endpoints
- Generate short 8-character unique IDs
- Prevent duplicate URLs by returning existing short URLs
- Validate URL input format
- Fast redirection to original URLs
- Track each visit with timestamps
- Count total clicks for each shortened URL
- View timestamp history for each visit
- Restrict Access: Added middleware to restrict access to logged-in users for URL-related routes.
- Cookie Parsing: Corrected cookie-parser usage to ensure proper session handling.
- JSON Middleware: Included JSON middleware for handling API requests.
- ✅ Stateless authentication with JWT
- ✅ Full-stack web development with Node.js
- ✅ Front-end design with EJS templating and CSS
- ✅ MVC architectural pattern implementation
- ✅ MongoDB database integration with Mongoose
- ✅ Express.js middleware usage
- ✅ Error handling in asynchronous Node.js code
- ✅ HTTP redirects and status codes
- ✅ Analytics tracking and reporting
- ✅ Modular code organization
- ✅ Responsive web design
- Custom short URL support
- QR code generation for shortened URLs
- Link expiration options
- Comprehensive test coverage
- Advanced analytics with geographical data
- Dark mode support
- Copy-to-clipboard functionality


