https://github.com/sunjay-dev/url-shortner-using-mongodb
A URL Shortner created using Mongodb
https://github.com/sunjay-dev/url-shortner-using-mongodb
mongodb mongoose nodejs url url-redirect url-shortener
Last synced: about 1 month ago
JSON representation
A URL Shortner created using Mongodb
- Host: GitHub
- URL: https://github.com/sunjay-dev/url-shortner-using-mongodb
- Owner: sunjay-dev
- License: mit
- Created: 2025-03-07T16:41:21.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-15T17:13:14.000Z (about 2 months ago)
- Last Synced: 2025-03-24T16:16:21.678Z (about 1 month ago)
- Topics: mongodb, mongoose, nodejs, url, url-redirect, url-shortener
- Language: JavaScript
- Homepage: https://i.sunjay.xyz/
- Size: 323 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# URL Shortener Using Mongodb
A simple URL shortener built with Node.js and Express.js that stores shortened URLs in MongoDB. This project allows users to create short URLs and redirect to the original URLs.
## Features
- Shorten long URLs and access them using a shorter link.
- Store and manage URL mappings in MongoDB.
- Track click statistics and visit history.
- Enhanced Error handling.## Technologies Used






- Express + Node.js
- shortid (For creating short id for url)
- Mongoose (For Storage)
- Ejs (For Frontend)## Installation
1. Clone the repository:
```bash
git clone https://github.com/sunjay-dev/URL-Shortner-Using-Mongodb
cd URL-Shortner-Using-Mongodb
npm install2. Set up environment variables in a `.env` file (see below).
3. Run the server:
```bash
npm start
```## Environment Variables
Create a `.env` file in the root directory and add the following:
```env
PORT=9000
JWT_SECRET=JWT_SECRET
mongoUri=MONGODB_URL
GITHUB_CLIENT_ID=GITHUB_CLIENT_ID
GITHUB_CLIENT_SECRET=GITHUB_CLIENT_SECRET
GITHUB_CALLBACK_URL=http://localhost:9000/user/auth/github/callback
GOOGLE_CLIENT_ID=GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET=GOOGLE_CLIENT_SECRET
GOOGLE_CALLBACK_URL=http://localhost:9000/user/auth/google/callback
```## Routes
The application provides the following routes:
### GET `/shortid`
- **Description**: Handles requests to the shortened URL and redirects to the original URL.
- **Parameters**:
- `shortId`: The short ID of the URL to redirect.
- **Response**:
- On success, redirects to the original URL.
- If the short ID does not exist, returns a 404 error.
### POST `/`
- **Description**: Creates a new shortened URL.
- **Request Body**:
- `url`: The original URL to be shortened.
- **Response**:
- Returns a JSON object containing the shortened URL.### POST `/custom`
- **Description**: Creates a new shortened URL with a custom alias.
- **Request Body**:
- `url`: The original URL to be shortened.
- `custom`: The custom alias for the shortened URL.
- **Response**:
- Returns a JSON object containing the shortened URL with the custom alias.
- If the custom alias is already in use, returns an 409 error message.### GET `/api/details?url=shortId`
- **Description**: Provides details about shortened url.
- **Parameters**:
- `shortId`: The short ID of the URL to retrieve.
- **Response**:
- On success, returns a JSON object containing number of click, original url, and array of lastOpened times.
- If the short ID does not exist, returns a 404 error.## Contributing
Feel free to submit issues or pull requests if you have suggestions for improvements.