The app name is anagram of Nest. The basic concept is create a free API for learning purposes. In my school we learn Python, and this created specially for my school. But I think, it's very useful for other schools for teaching how RESTAPI-s works, or everyone else :) It uses NestJS under the hood. Supports basic HTTP methods (GET, POST, PATCH, DELETE). I will add Put method in the future.
You will need:
- Docker
- Terminal (zsh, bash, powershell)
- Git
- A Weather API key
- Register to visualcrossing, and you will get an api key. Copy the given API key.
- After you installed Docker, you need to clone this repo.
With HTTPS:
git clone https://github.com/Floriian/sten.git
With SSH:
git clone https://github.com/Floriian/sten.git
- Navigate to git repo's folder, and edit the docker-compose file:
The app is listening on default port 3000. If the port is busy, it runs on port 5400. If theese ports, is not good for you, change (or add a new port) the first value of ports, and write an another expose children with the choosed port. - Start the app:
docker compose up -d
, ordocker-compose up -d
- Verify the app is running.
docker ps
it shows something similar to this: - Swagger: open browser, and type (with default port): http://localhost:3000
You will need:
- A Weather API key
- Shell
- Git
- PostgreSQL
- NodeJS
- Get the Weather API key, and save somewhere.
- Install NodeJS & PostgreSQL on your computer.
- Create a database in the Postgres server.
- Clone the git repo.
- In terminal, navigate to Gitrepos folder, and type:
npm install
- Open .env file, and change WeatherAPI key to your API key.
- Modify your connection string, with your PostgreSQL authentication info.
DATABASE_URL="postgresql://<POSTGRES_USERNAME>:<POSTGRES_PASSWORD>@localhost:<POSTGRES_PORT>/<POSTGRES_DATABASE>?schema=public"
- After you changed the .env file, enter the following command:
npm run build
- After the application is builded, start the server.
npm run start:prod
- The port is in the console, where is the app listening.
GET /animal
It returns all animals in the db.
Option | Value |
---|---|
Params | None |
Request body | Required |
Example responses
- Response (Code 200):
[ { "id": 0, "name": "The animal's name", "specie": "The animal's specie", "age": 10 }, { "id": 1, "name": "The animal's name", "specie": "The animal's specie", "age": 11 } ]
- Response (Code 404): No animals in the database.
GET /animal/:id
It returns one animal, with a given ID.
Examples:
GET /animal/1
Example responses
-
Response (Code 200):
{ "id": 1, "name": "Lessie", "specie": "Fox", "age": 18 }
-
Response (Code 404):
No animal found in database. -
Response (Code 400):
This response happens, when the param not int, It will return a body
POST /animal
Option | Value |
---|---|
Params | None |
Request body | Required |
Request body options:
Key | Type | Required? |
---|---|---|
name | string | ✅ |
specie | string | ✅ |
age | int | ✅ |
Example request:
POST /animal
Content-Type: application/json
{
"name": "Radír",
"specie": "Mopsz",
"age": 12
}
Example responses
- Response (Code 201):
The animal has been created, and returns the created animal data.{ "id": 1, "name": "Radír", "specie": "Mopsz", "age": 12 }
- Response (Code 400)
This response happens, when the body is failed the validation test.
PATCH /animal/:id
It will update animal by id, and returns the animal with updated data.
Option | Value |
---|---|
Params | Required |
Request body | Required |
Request body options:
Key | Type | Required? |
---|---|---|
name | string | ❌ |
specie | string | ❌ |
age | int | ❌ |
Example request:
PATCH /animal/1
Content-Type: application/json
{
"name": "Lessie"
}
Example responses
-
Response (Code 202)
{ "name": "Lessie", "specie": "Fox", "age": 10 }
-
Response (Code 404):
No animal found with this ID. -
Response (Code 400):
This response happens, when the body is failed the validation test.
DELETE /animal/:id
It deletes animal by id, and return the deleted animal's data.
Option | Value |
---|---|
Params | Required |
Request body | None |
Example request
DELETE /animal/1
Example responses
- Response (Code 200)
{ "id": 1, "name": "Radír", "specie": "Mopsz", "age": 10 }
- Response (Code 404)
No animal fond with the given ID. - Response (Code 400)
This response happens, when the param not int, It will return a body