r2c-url-shortener
Version:
Quick, Reusable, Reliable, Collision-free URL shortener with Redis and Postgres support
124 lines (88 loc) β’ 2.68 kB
Markdown
# π§© r2c-url-shortener
A lightweight, pluggable Node.js utility for shortening URLs using **PostgreSQL**, **Redis**, and **SHA-256 hashing**. Perfect for building your own short URL service or integrating into existing backend microservices.
## π Features
- π Generates unique short URLs with collision handling
- π§ Uses Redis for fast lookup caching
- π Persists URLs in PostgreSQL
- βοΈ Configurable via user-defined options
- β Handles edge cases with custom error handling
## π¦ Installation
```bash
npm install r2c-url-shortener
```
## π§° Usage
### 1. Initialize PostgreSQL and Redis connections:
```js
const { initPostgres, initRedis } = require('r2c-url-shortener');
initPostgres({
username: 'postgres',
password: 'your_password',
host: 'localhost',
database: 'url-shortener',
port: 5432,
});
initRedis({
url: 'redis://localhost:6379',
});
```
### 2. Compress a Long URL
```js
const { compress } = require('r2c-url-shortener');
const func = async() => {
const urlObject = {
longUrl: "https://www.youtube.com/watch?v=h4i7aRqIeRA&ab_channel=FarawayVillage"
};
const options = {
baseUrl: "your_url", // Your URL e.g. http://abc.com
maxCollisionAttempts: 50, // optional
urlCachingInMinutes: 45 // optional
};
const response = await compress(urlObject, options);
console.log("Compressed URL: ", response.shortUrl);
// Example output URL: https://abc.com/5f75f76
};
```
---
### 3. Redirect the short URL generated using above compress()
```js
const { redirect } = require('r2c-url-shortener');
const func = async() => {
await redirect({ shortUrl: "https://{your_domain}/5f75f76" });
};
```
## ποΈ Dependencies
- `pg` for PostgreSQL connection
- `redis` for in-memory key-value caching
- `uuid` and `moment` for ID & timestamp management
- Nodeβs native `crypto` (WebCrypto API) for hashing
## π Database Schema
You must have a `urls` table in PostgreSQL:
```sql
CREATE TABLE urls (
id UUID PRIMARY KEY,
short_code TEXT NOT NULL UNIQUE,
long_url TEXT NOT NULL,
created_at TIMESTAMP NOT NULL
);
```
## π‘ Error Handling
All errors are wrapped in a custom `AppError` class with `statusCode` and `message`. You can handle them in your main app like this:
```js
try {
await compress(...);
} catch (err) {
console.error(err.statusCode, err.message);
}
```
## π License
MIT Β© Almonds Technology Corporation
π§ almonds.corporation@gmail.com
### Comments, suggestions and bug reports are welcomed