105 lines
2.1 KiB
Markdown
105 lines
2.1 KiB
Markdown
# 🔗 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
|
|
|
|
```bash
|
|
npm install digi-url-shortener
|
|
```
|
|
|
|
Make sure you have Node.js v18+ installed.
|
|
|
|
## 📁 Project Structure
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
DB_PATH=./database/links.db
|
|
```
|
|
|
|
## 🚀 Usage
|
|
|
|
### Import Functions
|
|
|
|
```js
|
|
const {
|
|
getShortenUrlCode,
|
|
getOriginalUrlByShortCode,
|
|
deleteExpiredShortUrlsCodes,
|
|
} = require("./interface");
|
|
```
|
|
|
|
### Create a Short URL
|
|
|
|
```js
|
|
const shortCode = await getShortenUrlCode({
|
|
longUrl: "https://example.com",
|
|
expiryInDays: 7,
|
|
codeLength: 10,
|
|
});
|
|
console.log("Short code:", shortCode);
|
|
```
|
|
|
|
### Get Original URL
|
|
|
|
```js
|
|
const originalUrl = await getOriginalUrlByShortCode("shortCode123");
|
|
console.log("Original URL:", originalUrl);
|
|
```
|
|
|
|
### Start Expired Link Cleanup
|
|
|
|
```js
|
|
deleteExpiredShortUrlsCodes();
|
|
```
|
|
|
|
### Example
|
|
|
|
```js
|
|
(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
|