Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener

A small URL shortener built with Node.js, Express, and PostgreSQL.

Submit a long http or https URL, get back a short URL, then visit the short URL to redirect back to the original destination.

Features

  • POST /shorten creates a short URL
  • GET /:code redirects a short code to the stored long URL
  • PostgreSQL storage with a committed schema.sql
  • Random 6-character short codes using A-Z, a-z, and 0-9
  • Duplicate short-code retry handling using the database UNIQUE constraint
  • URL validation with Node's built-in URL parser
  • Only allows http: and https: URLs
  • Rate limiting on POST /shorten
  • Environment variables loaded with dotenv
  • Configurable server port with PORT
  • Configurable public base URL with BASE_URL
  • Interactive CLI client in app.js

Requirements

  • Node.js 18+
  • npm
  • PostgreSQL

Install

npm install

Database setup

Create a PostgreSQL database, then run:

psql your_database_name < schema.sql

Current schema:

CREATE TABLE IF NOT EXISTS url_mapping (
    id SERIAL PRIMARY KEY,
    long_url TEXT NOT NULL,
    short_code TEXT NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Environment setup

Create a .env file in the project root:

DATABASE_URL=postgres://username:password@localhost:5432/your_database_name
BASE_URL=http://localhost:3000

Run the server

Development mode with auto-restart:

npm run dev

Basic start:

npm start

Using the CLI client

Start the server first, then run:

node app.js

Enter a URL when prompted:

Enter your long URL: https://example.com/some/long/path

Example output:

{
  "message": "URL shortened successfully",
  "shortUrl": "http://localhost:3000/abc123"
}

The CLI posts to:

${BASE_URL}/shorten

So BASE_URL must point to your running server.

API usage

Shorten a URL

POST /shorten
Content-Type: application/json

Request body:

{
  "longUrl": "https://example.com/some/long/path"
}

Success response:

{
  "message": "URL shortened successfully",
  "shortUrl": "http://localhost:3000/abc123"
}

Invalid URL response:

{
  "error": "Invalid URL"
}

Server failure response:

{
  "error": "Failed to shorten URL"
}

Redirect a short URL

Visit:

http://localhost:3000/abc123

How it works

app.js/client -> POST /shorten -> server.js -> INSERT into url_mapping -> JSON shortUrl
browser       -> GET /:code    -> server.js -> SELECT from url_mapping -> redirect

Project files

File Purpose
server.js Express app, database connection, routes, rate limiter, short-code generation
app.js Interactive CLI client
schema.sql PostgreSQL schema

Known limitations

  • Same long URL can be shortened multiple times into different codes.
  • Redirect errors are plain text, while /shorten errors are JSON.
  • Stored redirect targets are trusted after insertion; there is no re-validation before res.redirect.
  • No click tracking, expiry, delete endpoint, metadata endpoint, custom aliases, tests, linter, or UI.
  • Not production-ready without more security work, especially around redirect abuse, CORS, and headers.

About

URL Shortener using expressJS and pgsql

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages