Redirecty is a high-performance, lightweight URL shortener built with Hono and running on Cloudflare Workers. It uses a Neon serverless Postgres database for data storage, providing a scalable and cost-effective solution.
- Fast Redirects: Built on Cloudflare's edge network for minimal latency.
- Custom Short Codes: Users can provide their own custom short codes.
- Automatic Code Generation: If no custom code is provided, a unique 8-character code is generated using
nanoid. - Title Fetching: Automatically fetches the title of the original URL for better link management.
- Visit Analytics: Tracks total visits, referrers, and visitor countries for each link.
- Scalable: Serverless architecture for both the application and the database.
- Framework: Hono
- Platform: Cloudflare Workers
- Database: Neon (Serverless Postgres)
- Language: TypeScript
Before you begin, ensure you have the following:
- Node.js (v18 or later)
- A Cloudflare account
- Wrangler CLI installed and authenticated
- A Neon account with a new project and database created.
git clone <repository-url>
cd redirecty_backend/redirectynpm install- Log in to your Neon account and create a new project.
- In your project's dashboard, go to the SQL Editor.
- Copy the contents of the
schema.sqlfile from the root of this repository and run it in the SQL Editor to create theurlstable. - Navigate to the Connection Details section of your dashboard and find your database connection string (it should look like
postgresql://...). You will need this for the next step.
This project uses a wrangler.example.jsonc as a template. You will need to create your own wrangler.jsonc file to run the application.
- Copy the example file:
cp wrangler.example.jsonc wrangler.jsonc
- Open
wrangler.jsoncand replace the placeholder value forDATABASE_URLwith your actual Neon database connection string. Thewrangler.jsoncfile is included in.gitignoreand will not be committed to the repository.
Alternatively, for production, you can use Wrangler secrets.
# Replace <YOUR_DATABASE_URL> with the connection string from Neon
wrangler secret put DATABASE_URLIf you are using this backend with a separate frontend application, you must add your frontend's URL to the CORS allowlist.
- Open
redirecty/src/index.ts. - Locate the
corsmiddleware section. - Add your frontend application's domain to the
originarray:// ... app.use('/*', cors({ origin: ['http://localhost:5173', 'https://your-frontend-domain.com'], // Add your frontend URL here // ... })); // ...
Once your worker is configured, you can deploy your application to Cloudflare Workers.
npm run deployWrangler will build and deploy your Hono application. After deployment, it will show you the URL where your worker is live (e.g., redirecty.<your-subdomain>.workers.dev).
To run the application on your local machine for development:
npm run devThis will start a local server, typically on http://localhost:8787.
Creates a new short URL.
Body:
{
"url": "https://example.com/a-very-long-url-to-be-shortened",
"shortCode": "custom-code", // Optional: your custom short code
"title": "Custom Title" // Optional: a title for the link
}Response (Success):
{
"success": true,
"shortUrl": "https://<your-worker-url>/custom-code",
"code": "custom-code"
}Redirects to the original full URL associated with the short code and records analytics data.
Retrieves a list of all created short URLs, along with their visit counts and titles.
Response (Success):
{
"success": true,
"urls": {
"example": {
"full_url": "https://example.com/",
"visit_count": 10,
"title": "Example Domain"
}
}
}Retrieves detailed analytics for a specific short code.
Response (Success):
{
"success": true,
"analytics": {
"shortCode": "example",
"originalUrl": "https://example.com/",
"totalVisits": 10,
"referrers": {
"google.com": 5,
"direct": 5
},
"countries": {
"US": 8,
"CA": 2
},
"lastUpdated": "2023-10-27T10:00:00.000Z",
"title": "Example Domain"
}
}This project is intended as a template. For a production environment, you should secure the administrative endpoints (/api/urls and /api/analytics/:code) to prevent unauthorized access to your link data. This can be done by implementing an authentication mechanism (e.g., using JWTs, Cloudflare Access, or simple API keys).