This project is presented for an article I wrote on Medium, providing a tutorial about a simplified example of clean architectre using Node.js, Express, Mongodb, Redis.
Node.js REST API clean architecture sample project documentation.
- Node js (18^).
- Express.js (4^)
- mongodb (8^)
- Authentication
- Authorization
- Protected routes
- Role-based access control (RBAC)
- Database seed
- Cache db
- clone project
- create .env file in the root directory and fill this environment variables:
- PORT
- MONGODB_URL
- JWT_SECRET
- AUTHORIZATION_KEY
- SEED_ADMIN_USERNAME
- SEED_ADMIN_PASSWORD
- CACHE_DB_HOST (Optional for cache db)
- CACHE_DB_PORT (Optional for cache db)
- CACHE_DB_PASSWORD (Optional for cache db)
- run:
$ npm install
$ npm run db:seed
$ npm run dev
-
Headers
'Authorization-Key': string
'admin-access-token': "Bearer ${jwtToken}"
(For admin routes)'x-access-token': "Bearer ${jwtToken}"
(For user routes) -
Responses
-
Unauothorized Client
Status:
401
{ "success": false, "message": string }
-
Forbidden
Status:
403
{ "success": false, "message": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Not Acceptable
Status:
406
{ "success": false, "error": string }
-
Internal Server Error
Status:
500
{ "success": false, "message": string }
-
-
Request
POST
/auth/user/sign-up
note:
access-token header is not required.
-
Body
firstName: string, *required. lastName: string, *required. email: string, pattern(email), *required. password: string, min(8), *required. confirmPassword: string, *required. phoneNumber: string, pattern(numbers), *required.
-
Responses
-
Created
Status:
201
{ "success": true, "message": string, "user": User }
-
Conflict
Status:
409
{ "success": false, "message": string }
-
-
Request
POST
/auth/user/sign-in
note:
access-token header is not required.
-
Body
phoneNumber: string, *required. password: string, *required.
-
Responses
-
Success
Status:
200
{ "success": true, "message": string, "token": string, "user": User }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Bad Request
Status:
400
{ "success": false, "message": string }
-
-
Request
POST
/auth/manager/sign-in
note:
access-token header is not required.
-
Body
username: string, *required. password: string, *required.
-
Responses
-
Success
Status:
200
{ "success": true, "message": string, "token": string, "manager": Manager }
-
Not Found
Status:
404
{ "success": false, "message": string, }
-
-
Request
PATCH
/auth/forgot-password
note:
access-token header is not required.
-
Body
phoneNumber: string, *required.
-
Responses
-
Success
Status:
200
{ "success": true, "message": string, "userId": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
-
Request
POST
/manager
-
Body
username: string, pattern(email), *required. password: string, min(8), *required. confirmPassword: string, *required. role: string, valid('Manager', 'Director', 'Monitor'), *required. phoneNumber: string, pattern(numbers), *required.
-
Role
Manager
-
Responses
-
Created
Status:
201
{ "success": true, "message": string, "manager": Manager }
-
Conflict
Status:
409
{ "success": false, "message": string }
-
-
Request
GET
/manager
- Role
Director
-
Query
username: string. role: string, valid('Manager', 'Director', 'Monitor'). phoneNumber: string, pattern(numbers)
-
Responses
-
Success
Status:
200
{ "success": true, "totalResults": Int, "results": [Manager] }
-
-
Request
GET
/manager/{id}
-
Role
Director
-
Responses
-
Success
Status:
200
{ "success": true, "manager": Manager }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
-
Request
DELETE
/manager/{id}
-
Role
Manager
-
Responses
-
Success
Status:
200
{ "success": true, "message": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
-
Request
PATCH
/manager/{id}
-
Role
Manager
-
Body
username: string. role: string, valid('Manager', 'Director', 'Monitor'). phoneNumber: string, pattern(numbers).
-
Responses
-
Created
Status:
201
{ "success": true, "message": string, "manager": Manager }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Conflict
Status:
409
{ "success": false, "message": string }
-
-
Request
GET
/user
-
Query
firstName: string. lastName: string. email: string, pattern(email). phoneNumber: string, pattern(numbers) page: Int, min(1). size: Int, min(1).
-
Role
Director
-
Responses
-
Success
Status:
200
{ "success": true, "page": Int, "totalPages": Int, "pageResults": Int, "totalResults": Int, "results": [User] }
-
-
Request
GET
/user/{id}
-
Role
Director
-
Responses
-
Success
Status:
200
{ "success": true, "user": User }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
-
Request
DELETE
/user/{id}
-
Role
Manager
-
Responses
-
Success
Status:
200
{ "success": true, "message": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
-
Request
PATCH
/user/{id}
-
Role
Director
-
Body
firstName: string. lastName: string. email: string, pattern(email).
-
Responses
-
Created
Status:
201
{ "success": true, "message": string, "user": User }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Conflict
Status:
409
{ "success": false, "message": string }
-
-
Request
PATCH
/user/{id}/profile
-
Role
Director
-
Body
firstName: string. lastName: string. email: string, pattern(email).
-
Responses
-
Created
Status:
201
{ "success": true, "message": string, "user": User }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Conflict
Status:
409
{ "success": false, "message": string }
-
Bad Request
Status:
400
{ "success": false, "message": string }
-
-
Request
PATCH
/user/{id}/verify-account
-
Body
code: string.
-
Responses
-
Created
Status:
201
{ "success": true, "message": string, "token": string, "user": User }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Bad Request
Status:
400
{ "success": false, "message": string }
-
-
Request
PATCH
/user/resend-code
note:
access-token header is not required.
-
Body
userId: string, *required.
-
Responses
-
Success
Status:
200
{ "success": true, "message": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
-
Request
PATCH
/user/{id}/reset-password
note:
access-token header is not required.
-
Body
code: string, *required. password: string, min(8), *required. confirmPassword: string, *required.
-
Responses
-
Created
Status:
201
{ "success": true, "message": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
Bad Request
Status:
400
{ "success": false, "message": string }
-
-
Request
DELETE
/user/account
-
Body
phoneNumber: string, *required. password: string, *required.
-
Responses
-
Success
Status:
200
{ "success": true, "message": string }
-
Not Found
Status:
404
{ "success": false, "message": string }
-
{
id: string,
username: string,
role: string,
phoneNumber: string,
createdAt: Date,
updatedAt: Date
}
{
id: string,
firstName: string,
lastName: string,
email: string,
phoneNumber: string,
verified: Boolean,
otpCode?: string,
otpExpire?: Date,
createdAt: Date,
updatedAt: Date
}