2.1 KiB
2.1 KiB
🔗 Link Shortener Plugin
A simple, pluggable Node.js module for shortening URLs using SQLite, with support for custom expiry dates and automatic cleanup via cron jobs.
📦 Features
- ✅ Shorten long URLs with optional expiry dates
- ✅ Retrieve the original URL from a short code
- ✅ Auto-deletes expired links using a daily cron job
- ✅ Uses SQLite for lightweight persistence
- ✅ No external APIs required
🛠️ Installation
npm install digi-url-shortener
Make sure you have Node.js v18+ installed.
📁 Project Structure
link-shortener/
├── database/
│ └── links.db # SQLite DB (created automatically)
├── utils/
│ └── helperFunctions.js # Core logic (shorten, retrieve, cleanup)
├── dbConfig/
│ └── index.js # SQLite CRUD operations
├── index.js # Entry point or API setup
├── .env.example # Environment variables
├── package.json
└── README.md
⚙️ Configuration
DB_PATH=./database/links.db
🚀 Usage
Import Functions
const {
getShortenUrlCode,
getOriginalUrlByShortCode,
deleteExpiredShortUrlsCodes,
} = require("./interface");
Create a Short URL
const shortCode = await getShortenUrlCode({
longUrl: "https://example.com",
expiryInDays: 7,
codeLength: 10,
});
console.log("Short code:", shortCode);
Get Original URL
const originalUrl = await getOriginalUrlByShortCode("shortCode123");
console.log("Original URL:", originalUrl);
Start Expired Link Cleanup
deleteExpiredShortUrlsCodes();
Example
(async () => {
const shortCode = await getShortenUrlCode({
longUrl: "https://example.com",
expiryInDays: 5,
codeLength: 10,
});
console.log("Short code is:", shortCode);
const original = await getOriginalUrlByShortCode(shortCode);
console.log("Original URL:", original);
})();
License 📝
This project is licensed under the ISC License.
Author ✍️
DigiMantra