Skip to content

Dev5022/url-shortener

Repository files navigation

URL Shortener API

A production-ready URL Shortener built with Node.js, Express, and SQLite. Dockerized and ready for deployment.

Features

  • Shorten URLs with validation
  • Custom aliases
  • Expiration dates
  • Click tracking (analytics)
  • Rate limiting
  • SQLite database with auto-migration
  • Docker support with multi-stage builds (runs tests during build)

Prerequisites

  • Node.js >= 18
  • Docker & Docker Compose (optional)

Setup & Running Locally

  1. Install Dependencies

    npm install
  2. Configure Environment Copy .env.example to .env:

    cp .env.example .env
    # Edit .env if needed
  3. Run Tests

    npm test
  4. Start Server

    npm start
    # Or for development with nodemon:
    npm run dev

Docker

Build and Run

This will build the image (running tests inside) and start the service with a persistent SQLite volume.

docker-compose up --build

Build Only

docker build -t url-shortener .

API Usage

1. Shorten URL

POST /api/shorten

curl -X POST http://localhost:3000/api/shorten \
  -H "Content-Type: application/json" \
  -d '{ "longUrl": "https://www.google.com", "customAlias": "go-google", "expiresInDays": 7 }'

Response:

{
  "code": "go-google",
  "shortUrl": "http://localhost:3000/go-google",
  "longUrl": "https://www.google.com",
  "expiresAt": "2023-10-27T10:00:00.000Z",
  "createdAt": "2023-10-20T10:00:00.000Z"
}

2. Redirect

GET /:code

Opens the short URL in browser or redirects via curl.

curl -L -v http://localhost:3000/go-google

3. Get URL Info

GET /api/info/:code

curl http://localhost:3000/api/info/go-google

Response:

{
  "longUrl": "https://www.google.com",
  "createdAt": "...",
  "expiresAt": "...",
  "clicks": 5,
  "lastAccessedAt": "..."
}

4. Health Check

GET /health

curl http://localhost:3000/health

CI/CD Deployment (AWS EC2)

To deploy to AWS EC2 via GitHub Actions, configure the following GitHub Secrets:

  1. EC2_HOST: Public IP or DNS of your EC2 instance.
  2. EC2_USERNAME: SSH username (e.g., ubuntu or ec2-user).
  3. EC2_SSH_KEY: Private SSH key content (PEM).

Workflow Overview (conceptual):

  1. CI: On push to main, run npm test.
  2. CD:
    • Log in to Docker Registry (Docker Hub or ECR).
    • Build and push image: docker build -t myuser/url-shortener . && docker push myuser/url-shortener.
    • SSH into EC2.
    • Run docker pull myuser/url-shortener.
    • Run docker-compose up -d.

Environment Variables

Variable Default Description
PORT 3000 Server port
BASE_URL http://localhost:3000 Base URL for short links
DB_PATH ./data/urls.db Path to SQLite database file
APP_VERSION dev Application version tag
NODE_ENV development Environment (production/development)

About

Make Url according to your convenience

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors