Eric013 / node-url-shortener

A modern and lightweight URL shortener using Node.js, Fastify, Postgres and Redis.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node Url Shortener

A modern and lightweight URL shortener using Node.js, Fastify, Postgres and Redis.

Requirements

  1. Docker
  2. Docker compose

Getting Started

Create the .env file from .env.example, then fill the variables with the desired values:

cp .env.example .env

Install packages using docker:

docker run --rm -it \
-v ${PWD}:/usr/src/app \
-w /usr/src/app \
node:16-alpine npm i

Windows users should switch the PWD variable to your current directory. Alternatively, you can run npm install as follows:

docker-compose run --rm api npm install

Or if you have node installed in your system, install using npm:

npm install

In the root of project, run:

docker-compose up -d

run the migrations:

docker-compose run --rm api npm run migrate:up

View logs

docker logs -f api

API URL

  • local: http://localhost:3000

API Request

Endpoint HTTP Method Description
/ GET Healthcheck
/encode POST Encode URL
/:hash GET Redirect to encoded URL
/:hash PATCH Update private property
/:hash/stats GET Stats of encoded URL

Test API locally using curl

  • Healthcheck

Request

curl -i --request GET 'http://localhost:3000'

Response

{
  "message": "Node Url Shortener API is on fire"
}
  • Encode URL

Request

curl -i --request POST 'http://localhost:3000/encode' \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "url": "https://github.com/pinceladasdaweb/node-url-shortener",
    "private": false
  }'

Response

{
  "url": "https://github.com/pinceladasdaweb/node-url-shortener",
  "alias": "1C",
  "private": false,
  "count": 0,
  "created_at": "2021-06-11T17:39:10.020Z"
}
  • Update private property

Request

curl -i --request PATCH 'http://localhost:3000/1C' \
  --header 'Content-Type: application/json' \
  --data-raw '{,
    "private": true
  }'

Response

{
  "updated": true,
  "private": true
}
  • Get Stats of encoded URL

Request

curl -i --request GET 'http://localhost:3000/1C/stats'

Response

{
  "count": 16
}

About

A modern and lightweight URL shortener using Node.js, Fastify, Postgres and Redis.

License:MIT License


Languages

Language:JavaScript 94.7%Language:Dockerfile 3.8%Language:Shell 1.5%