A simple API in NodeJS. It has some basic functions, like:
- User registration and authentication using JWT
- Perform CRUD operations of Tasks
- Some basics business rules enforced
My main goal is to showcase some concepts, such as:
- Migrations and model relationship
- Authentication configuration
- Routes definitions
- SOLID and KISS principles on a MVC architecture
The main tech behind it is NodeJs and also some others Js libraries. For storaging, i'm using MySQL relational database
To run this application you must have installed on your environment:
NodeJS
- For the main applicationNPM
- For library and packages managementMySQL
- For storaging and accessing data
Having NPM and Node installed and configured on your device, git clone this rep then cd into the project's folder. First you need to create a copy of the config.json file. Run the following:
cp config/config.json.local config/config.json
And on this file, fill out the database connection information, such as:
{
"development": {
"username": "root",
"password": "root",
"database": "tasks_application",
"host": "localhost",
"dialect": "mysql"
}
}
Now create a local copy of the .env file with
cp .env.example .env
Like the config.json, fill out the database connection information:
DB_DATABASE=tasks_application
DB_USERNAME=root
DB_PASSWORD=root
DB_HOST=localhost
DB_PORT=3306
After that run the npm's installation command:
npm install
Before you run the app, you must migrate the app's database structure. Create the database with:
npx sequelize-cli db:create
And them create the tables with:
npx sequelize-cli db:migrate
Now start the service with:
npm start
If you see Server Started at 3000
as the last message then the app is good to go!
curl --location 'http://localhost:3000/register' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Philip Leotardo",
"email": "[email protected]",
"password": "123456"
}'
curl --location 'http://localhost:3000/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "[email protected]",
"password": "123456"
}'
For the following requests, replace the {token}
with the Token retrieved on the /login response payload, under the "token" attribute
Replace {task_id}
with a Task Id
curl --location 'http://localhost:3000/api/task/create' \
--header 'x-access-token: {token}' \
--header 'Content-Type: application/json' \
--data '{
"title": "My new Task",
"description": "This is a task",
"type": "feature"
}'
Replace {created_by}
with a User Id
Replace {status}
with any of the following: new, in_qa, in_dev, blocked, closed
Replace {type}
with any of the following: feature, bugfix, hotfix
If none of these filters are present this endpoint will retrieve all existing Tasks
curl --location 'http://localhost:3000/api/task/list?owner={created_by}&status={status}&type={type}' \
--header 'x-access-token: {token}'
curl --location 'http://localhost:3000/api/task/view/{task_id}' \
--header 'x-access-token: {token}'
curl --location --request PUT 'http://localhost:3000/api/task/update/{task_id}' \
--header 'x-access-token: {token}' \
--header 'Content-Type: application/json' \
--data '{
"description": "a different description",
"status": "blocked"
}'
curl --location --request DELETE 'http://localhost:3000/api/task/delete/{task_id}' \
--header 'x-access-token: {token}'
curl --location --request PUT 'http://localhost:3000/api/task/close/{task_id}' \
--header 'x-access-token: {token}'