Skip to content

mmedoo/nodejs-url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nodejs sequelize sqlite ejs express

URL Shortener 🔗

A simple and efficient URL shortener application that allows users to shorten long URLs.

Table of Contents

🌟 Introduction

This URL shortener is a web application that allows users to convert long URLs into short links.

🌳 Environment

- Name Link
Language        Node.js          Nodejs
Server        Exress.js          express
Database        SQLite          sqlite
ORM        Sequelize          sequelize
View Engine        EJS          ejs

🚀 Run Locally

Prerequisites

  • Node.js    Node.js

Note: No need to install SQLite on your local environment, SQLite npm package will do the job.

Steps

  1. Open the terminal and clone the repository:
git clone https://github.com/mmedoo/url-shortener.git
  1. Go to the app directory
cd url-shortener
  1. Install dependencies
npm install
  1. Run the server
npm start

🛠️ Usage

Once the server runs, terminal should log:

Server is listening on http://localhost:1234
  1. Open your browser and go to http://localhost:1234.
  2. Enter the long URL you want to shorten.
  3. Click the "Shorten" button to get the short URL.
  4. Click on displayed URL to copy.
  5. Paste it in your browser.

Congrats 🌟, you're redirected to your super long URL.

📬 API Endpoints

GET /c/?url={desired_url}

  • Creates a new key, if doesn't exist, for {desired_url}.
  • Responds with the key.

GET /{key}

  • Requests the URL corresponding to {key}.
  • Responds with a redirectng page to corresponding URL.

⚙️ Configuration (optional)

The application can be configured using environment variables. Here are the available options:

  • PORT: The port on which the application will run (default: 1234).
  • FORCE_DROP: Boolean value to determine wether the links database table is forcably dropped everytime the app initializes connection with the database (this determines if the data will be saved after closing the app) (default: false).

Here is how to create .env file to change these settings:

Linux

  1. Create the .env File: Open the terminal in app folder and run command:
touch .env
  1. Open the .env File for Editing: You can use any text editor of your choice, here is a way through the terminal:
nano .env
  1. Add environment variables and set them as you like:
PORT=1234
FORCE_DROP=false

Windows

  1. Create the .env File: Open the terminal in app folder and run command:
echo.>.env
  1. Open the .env File for Editing: You can use any text editor of your choice to open and edit the .env file. Run this command to use notepad:
notepad .env
  1. Add environment variables and set them as you like:
PORT=1234
FORCE_DROP=false