An API built in Node.js using the Serverless framework for deployment on AWS. This API integrates with JSONPlaceholder and offers custom endpoints, JWT authentication, and role-based authorization.
This project was designed as a solution to a technical challenge that involves:
- Integration with the JSONPlaceholder API.
- Authentication using JWT (JSON Web Tokens).
- Role-based authorization with middleware.
- Use of the
Serverless Framework
for deployment in AWS.
- Previous requirements: Make sure you have Node.js and npm installed.
- Clone this repository:
git clone https://github.com/edsonarios/node-serverless.git
- Navigate to the project folder and install the dependencies:
cd node-serverless npm install
- To start the API in development mode:
- Download the dynamodb zip file Link zip file to DynamoDB and put in place
.dynamodb
npm run dev http://localhost:3000
- To start the API in production mode:
-
Download the AWS CLI for manage the credentials Link AWS cli
-
Create user IAM with Access keys
ACCESS_KEY
andSECRET_KEY
aws configure # set the access and secret key npm deploy:prod
All endpoints, except the login endpoint, require authentication via JWT. Depending on the user's role, they will have access to different endpoints.
- Login
[POST] /login
: Authenticates a user and returns a JWT token.
- List Users
[GET] /users
: Gets a list of all users. Requiresadmin
role. - Get User
[GET] /users/{userId}
: Gets a specific user by their ID. Requiresadmin
role. - Create User
[POST] /users
: Create a new user. Requiresadmin
role.
- List Roles
[GET] /roles
: Gets a list of all roles. Requiresadmin
role. - Get Role
[GET] /roles/{roleId}
: Gets a specific role by its ID. Requiresadmin
role. - Create Role
[POST] /roles
: Create a new role. Requiresadmin
role.
- List Posts
[GET] /posts
: Gets a list of all posts. Requiresadmin
role. - Get Post
[GET] /posts/{postId}
: Gets a specific post by its ID. Requiresadmin
role.
- List Comments
[GET] /comments
: Gets a list of all comments. Requiresadmin
role. - Get Comment
[GET] /comments/{commentId}
: Gets a specific comment by its ID. Requiresadmin
role.
- Get My Data
[GET] /me
: Obtains the information of the authenticated user. Requirespersonal
role. - My Posts
[GET] /me/posts
: Gets the authenticated user's posts. Requirespersonal
role. - One of My Posts
[GET] /me/posts/{postId}
: Gets a specific post from the authenticated user. Requirespersonal
role. - Comments on one of My Posts
[GET] /me/posts/{postId}/comments
: Gets the comments on a specific post from the authenticated user. Requirespersonal
role.
To run the tests:
npm test
The tests cover the endpoints of: login, users, roles, posts, comments and me.
To access the Swagger documentation, start the server and visit the roote:
http://localhost:3000/
Aquí se detallan todos los puntos solicitados en el reto técnico y su estado de realización:
Link Produccion https://9anke0cyv4.execute-api.us-west-2.amazonaws.com
- Endpoints mínimos para el rol Personal
- Integración con una base de datos: Se utilizó DynamoDB
- Integración con JSONPlaceholder
- Uso del Serverless Framework
- Uso de Node.js con Javascript
- Uso de ORM: Se empleó dynamoose como ORM para DynamoDB.
- Respeto de las buenas prácticas de desarrollo
- Uso de patrones de diseño: Se implementaron los patrones MVC y Factory
- JWT para Autenticación
- Autorización basada en roles usando middleware
- Pruebas unitarias 16 Tests para todos los EndPoints
npm test
- Documentación con Open API/Swagger
- Despliegue sin errores en AWS
npm run deploy:prod
- Creación de tablas: Se crearon las tablas Users, Roles, y Counters para autoincremento de IDs.
Espero que esta documentación facilite la revisión y evaluación del proyecto. Quedo a disposición para cualquier consulta o aclaración.