Skip to content

NodeJS url shortener using express and postgreSQL database(xata.io recommended), Easy and simple to use

License

Notifications You must be signed in to change notification settings

Aarondoran/Node-URL-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Endpoint Badge for demo Forks Stars Releases Last Commit Website

DotLink - Simple URL Shortener

Docs & Live Demo: dotlink.js.org

DotLink is a minimal URL shortener that allows the creation of shortened links with optional custom aliases. Anyone can access shortened links, and the API also provides statistics such as click counts and original URLs.

Features

  • Custom aliases for shortened links
  • PostgreSQL database integration
  • Simple REST API with Express
  • Link analytics endpoint to check clicks and original URL
  • Custom 404 page for invalid or non-existent links

Setup

1. Clone the Repository

git clone https://github.com/Aarondoran/dotlink.git
cd dotlink

2. Install Dependencies

npm install

3. Configure Environment Variables

Create a .env file in the root directory and add the following:

PORT=3030
DATABASE_URL=postgresql://YOUR_USERNAME:YOUR_PASSWORD@YOUR_HOST/YOUR_DATABASE

Replace DATABASE_URL with your actual PostgreSQL connection string.

4. Set Up the Database

Create the urls table in your PostgreSQL database:

CREATE TABLE urls (
  id SERIAL PRIMARY KEY,
  short_id VARCHAR(255) NOT NULL,
  original_url TEXT NOT NULL,
  clicks INTEGER DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

5. Start the Server

Run manually:

node server.js

OR run with PM2 (recommended for production):

pm2 start server.js --name dotlink

API Endpoints

➤ Shorten a URL

Endpoint:

POST /api/shorten

Request Body:

{
  "originalUrl": "https://example.com",
  "customAlias": "myalias"
}

Response:

{
  "shortUrl": "http://yourdomain.com/myalias"
}

➤ Redirect to Original URL

Endpoint:

GET /:short_id

Example:

GET http://yourdomain.com/myalias

Redirects to:

https://example.com

➤ Check Link Information

Returns the original URL and click count for a shortened link.

Endpoint:

GET /check/:id

Response:

{
  "short_id": "myalias",
  "original_url": "https://example.com",
  "clicks": 42,
  "created_at": "2024-01-01T12:00:00.000Z"
}

License

This project is licensed under the MIT License.

About

NodeJS url shortener using express and postgreSQL database(xata.io recommended), Easy and simple to use

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published