This is a little Docs explaining the project's main pillars.
Please note that the data is saved directly into a local file inside the folder /data
To consume the API resources a password is required in the header of the request :
"x-password" : "IAI2"
All test files are inside the folder /tests
.
PASS tests/user.test.js
User Endpoint Tests
GET /api/user
✓ No password :: 400 error (29 ms)
✓ Incorrect password :: 401 error (3 ms)
✓ Get users list :: 200 success (9 ms)
POST /api/user
✓ No password :: 400 error (3 ms)
✓ Incorrect password :: 401 error (3 ms)
✓ Create new user {valid password} :: 201 success (37 ms)
✓ No password :: 400 error (2 ms)
✓ Incorrect password :: 401 error (2 ms)
✓ Create new user {password is not valid} :: 400 success (5 ms)
Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total
Snapshots: 0 total
Time: 0.839 s, estimated 1 s
Ran all test suites.
PASS tests/user.test.js
User Endpoint Tests
GET /api/user
✓ get :: No password :: 400 error (27 ms)
✓ get :: Incorrect password :: 401 error (4 ms)
✓ get :: Get users list :: 200 success (9 ms)
POST /api/user
✓ post :: No password :: 400 error (3 ms)
✓ post :: Incorrect password :: 401 error (3 ms)
✓ post :: Create new user {valid password} :: 201 success (37 ms)
✓ post :: No password :: 400 error (4 ms)
✓ post :: Incorrect password :: 401 error (3 ms)
✓ post :: Create new user {password is not valid} :: 400 success (5 ms)
Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total
Snapshots: 0 total
Time: 0.923 s, estimated 1 s
Ran all test suites.
The project is organized into the following directories: (only user's example)
- controllers/UserController.js: Contains the logic for handling user-related routes and interactions with the model.
- models/UserModel.js: Defines the User model that represents user data. It interacts with the data stored in the
data/users.json
file.
- routes/UserRoute.js: Defines routes for user-related operations, utilizing the
userController
and applying validation usingexpress-validator
. Please note that validators are some sorts of middleware ==> verification functions (verifyAuth, verifyToken...)
- data/users.json: A JSON file where user data is stored. The
userModel
reads and writes data to this file.
- validators/UserValidator.js: Defines validation rules for user-related routes using
express-validator
. These rules are used to validate incoming request data.
- Navigate to the project directory:
cd xxxx
- Install dependencies:
npm install
- Start the application:
npm start
The project follows the MVC pattern to organize code and responsibilities. It focuses on API-based interactions without views.
Controllers in the controllers
directory contain route handlers and business logic. They use the joi
library to validate incoming request data.
Models in the models
directory define data structures and interact with data sources. In this project, the userModel
handles user-related data operations.
The data/users.json
file stores user data in JSON format. The userModel
reads from and writes to this file.
The validators/userValidator.js
file defines validation rules for user-related routes using joi
.
Even if the client sends multiple pieces of information that are not required or optional, as specified in the Joi
validator, the request will be refused immediately.