Skip to content

Repository files navigation

Express Starter App

This is a basic Express application that includes a URL shortener service. The application is written in JavaScript and uses npm for package management. This project implements a simple URL shortener service using Node.js, Express, and PostgreSQL.

Current Functionality

The application currently has two routes:

  1. POST /shorten: This route accepts a URL and returns a shortened URL. Currently, it returns a static shortened URL.
  2. GET /:shortUrl: This route accepts a shortened URL and redirects to a static URL.

The short URL mappings are stored in a PostgreSQL database.

Implemented Functionality

  1. POST /shorten
  • Accepts a JSON body containing a valid URL.
  • Checks if the URL already exists in the database.
  • If it does, returns the same short URL.
  • If not, generates a new unique short code.
  • Stores the short_url and original_url in the database.
  • Returns the shortened URL.
  1. GET /:shortUrl
  • Looks up the short code in the database.
  • Redirects to the original URL if found.
  • Returns 404 if not found.

How to start

  1. Open the repository in Codespaces

    Click Code → Create Codespace on main.

  2. Install dependencies

  • npm install
  • sudo apt update
  • sudo apt install -y postgresql-client
  1. Start PostgreSQL using Docker (inside Codespaces)

    docker compose up -d

    Confirm it’s running: docker ps

  2. Connect to PostgreSQL

    psql -U urluser -h localhost -d urlshortener

    Password: urlpass

  3. Create the required table

    CREATE TABLE urls ( id SERIAL PRIMARY KEY, short_url VARCHAR(10) UNIQUE NOT NULL, original_url TEXT NOT NULL );

  4. Start the application

    npm start

    Codespaces will prompt you to make port 3000 public.

    • Click Make Public → Open in Browser.

    • Copy the 3000 port URL for testing.

    • Your API base URL will look like: https://<your-id>-3000.app.github.dev

Testing the API with Postman

  1. Test POST /shorten

    Method: POST

    Go to Ports and turn port 3000 public and copy the local address - this will be the URL:

    URL: https://<your-id>-3000.app.github.dev/shorten

    Body → raw → JSON { "url": "https://google.com" }

    Example Response:{ "shortUrl": "https://<your-id>-3000.app.github.dev/AbC123" }

    Sending "https://google.com" again returns the same short URL.

  2. Test GET /:shortUrl

    Method: GET

    URL: https://<your-id>-3000.app.github.dev/AbC123

    Expected result: Redirect

    Browser takes you to https://google.com

    In Postman, turn Follow redirects OFF to see the redirect header.

  3. Testing invalid inputs

    Missing body:

    {}

    Returns: 400 Bad Request

    Invalid URL: { "url": "not-a-url" }

    Returns: 400 Bad Request

Resetting the Database (Optional)

To delete all stored URLs: DELETE FROM urls;

About

This is a basic Express application that includes a URL shortener service.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages