A modern and lightweight URL shortener using Node.js, Fastify, Postgres and Redis.
- Docker
- Docker compose
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
local
:http://localhost:3000
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 |
Request
curl -i --request GET 'http://localhost:3000'
Response
{
"message": "Node Url Shortener API is on fire"
}
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"
}
Request
curl -i --request PATCH 'http://localhost:3000/1C' \
--header 'Content-Type: application/json' \
--data-raw '{,
"private": true
}'
Response
{
"updated": true,
"private": true
}
Request
curl -i --request GET 'http://localhost:3000/1C/stats'
Response
{
"count": 16
}