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.
- 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.
- Express + Node.js
- shortid (For creating short id for url)
- Mongoose (For Storage)
- Ejs (For Frontend)
-
Clone the repository:
git clone https://github.com/sunjay-dev/URL-Shortner-Using-Mongodb cd URL-Shortner-Using-Mongodb npm install
-
Set up environment variables in a
.env
file (see below). -
Run the server:
npm start
Create a .env
file in the root directory and add the following:
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
The application provides the following routes:
- 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.
- Description: Creates a new shortened URL.
- Request Body:
url
: The original URL to be shortened.
- Response:
- Returns a JSON object containing the shortened URL.
- 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.
- 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.
Feel free to submit issues or pull requests if you have suggestions for improvements.