KoaCRUD is an API that manages users, its registrations, listings, updates and deletions. This app uses the programming language Javascript, its framework Koa.js, the database SQLite3 the APIs documenter Swagger and the test libs Mocha and Chai.
-
The code versioning Git,
-
A code editor, also known as IDE. For instance, Visual Studio Code (VSCode),
-
A client API REST program. Insomnia or Postman, for instance,
-
Javascript's runtime enviroment Node.js,
-
And Node.js' package manager NPM:
npm install -g npm
git clone https://github.com/AndreKuratomi/KoaCRUD.git
WINDOWS:
Obs: In case of any mistake similar to this one:
unable to access 'https://github.com/AndreKuratomi/KoaCRUD.git': SSL certificate problem: self-signed certificate in certificate chain
Configure git to disable SSL certification:
git config --global http.sslVerify "false"
Enter the directory:
cd KoaCRUD
Open your app for your IDE:
code .
Install its dependencies:
npm install
WINDOWS:
In case any error similar to the one bellow be returned:
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 'C:\\Users\\KoaCRUD\\lorem\\ipsum\\dolor\\etc'
HINT: This error might have occurred since this system does not have Windows Long Path support enabled. You can find information on how to enable this at https://pip.pypa.io/warnings/enable-long-paths
Run cmd as adminstrador with the following command:
reg.exe add HKLM\SYSTEM\CurrentControlSet\Control\FileSystem /v LongPathsEnabled /t REG_DWORD /d 1 /f
And run the application:
npm run dev
The app will run with local server localhost: or the default port 3000. Write after it the application routes or endpoint's as we are going to see bellow.
For running the app's tests use the following command:
npm test
touch .env
Inside it we need to define our enviroment variable PORT:
PORT=port
Obs: the .env infos cannot be shared! The arquive is already at .gitignore for not being pushed to the repository.
After everything is well installed use your API Client for the following routes:
User Registration (POST method): /user (or localhost:3000/user)
Example to be used as requisition body:
{
"age": "32",
"cpf": "00000000000",
"email": "[email protected]",
"nome": "João da Silva"
}
And the expected response:
Status: 201 CREATED
{
"message": "Data added successfully!"
}
In case of underage user registration the expected answer must be:
Status: 403 UNAUTHORIZED
{
"message": "Unauthorized! Users under 18 are not allowed."
}
Registered users listing (GET method): /users (or localhost:3000/users)
Example to be used as requisition body:
(No body requisition)
And the expected response:
Status: 200 OK
[
{
"id": 1,
"age": "32",
"cpf": "00000000000",
"email": "[email protected]",
"nome": "João da Silva"
}
]
User listing data (GET method): /user/id** (ou localhost:3000/users/id**)
**fill with the previously registerd user id.
Example to be used as requisition body:
(No body requisition)
And the expected response:
Status: 200 OK
[
{
"id": 1,
"age": "32",
"cpf": "00000000000",
"email": "[email protected]",
"nome": "João da Silva"
}
]
In case user is not found by id the expected answer must be:
Status: 404 NOT FOUND
{
"message": "User not found!"
}
User data update (PATCH method): /user/id** (or localhost:3000/users/id**)
**fill with the previously registerd user id.
Only the 'age' and 'email' fields can be updated.
Example to be used as requisition body:
{
"age": 35
}
In case the user is already registered in SQLite the response will be:
Status: 200 OK
{
"message": "User successfully updated!"
}
Otherwise the response will be:
Status: 404 NOT FOUND
{
"message": "User not found!"
}
If neither "age" nor "email" fields will be used the response will be:
Status: 400 BAD REQUEST
{
""message": "Invalid params! Must be or age or email or both!"
}
User registered deletion (DELETE method): /user/id** (or localhost:3000/users/id**)
**fill with the previously registerd user id.
Example to be used as requisition body:
(No body requisition)
In case the user is already registered in SQLite the response will be:
Status: 204 NO CONTENT
(No body requisition)
Otherwise the response will be:
Status: 404 NOT FOUND
{
"message": "User not found!"
}
API documentation by Swagger: /swagger (or localhost:3000/swagger)
This API is only for didatic purposes, not commercial.