Author: Agustín Avila Humerez
This project is an application developed with Express.js, as part of a technical test for the application for the position of Backend Developer (Node.js). It consists of a server that exposes a REST API, which provides a series of endpoints that allow interaction between a database (MongoDB) and an external API, managing the corresponding routes and middlewares.
To carry out this project, the guidelines indicated in the instructions provided were followed, which can be consulted at the following link:Instructions
Additionally, it was decided to use TypeScript together with ESLint and Prettier to improve both the development experience and the scalability and quality of the final code.
Finally, some basic tests were implemented as an example, using jest and supertest.
The main dependencies used in the project are the following:
- Node.js
- Express.js
- Mongoose - Typegoose
- Axios
- json-2-csv
- class-validator - class-transformer
- @hapi/boom
- compression
- cors
- helmet
- winston
NOTE: For a complete list of dependencies, see the package.json file.
It is required to have installed:
git clone https://github.com/aa-avila/searchmas-api-node-express-docker.git
cd searchmas-api-node-express-dockeryarn installFor initial setup it is necessary to create a .env file and copy the specified environment variables into .env.example.
This can be done manually, or by using the following command in the console:
cp .env.example .envFor the purposes of this technical test, the values provided as examples are functional for development use. So the MongoDB URI provided points to a database hosted in the cloud using MongoDB Atlas.
However, it is possible to edit the .env file as desired to provide other values that fit the user's particular case.
Build the app for the first time:
yarn buildTo start the server in development mode, use the following command:
yarn devThis command allows you to reinitialize your application whenever changes are made to the code, which helps speed up development iteration.
To start the server in production mode:
yarn startThis command performs a clean compilation of the code and then starts the server, assigning the value 'production' to the NODE_ENV environment variable.
-
Build the Docker image:
docker build -t api-searchmas .As a result, an image named
api-searchmaswill be created. -
Start the container:
docker run -p 3000:3000 --env-file=.env api-searchmas
This will run the server inside a Docker container and will be available at http://localhost:3000.
NOTE: To create the image and run the project using Docker, you must have previously installed Docker
NOTE: Ports can be modified according to the needs of the environment.
Run tests using the following command:
yarn test- Receives a request to query an external API and saves in the database: the requester's data along with part of the data obtained.
- This query consists of obtaining information about a random character, belonging to the television series "Rick and Morty". For this, the following API is used: Rick And Morty API
Payload Schema
| Property | Type | Description |
|---|---|---|
requester* |
string |
Requester's name |
requestReason? |
string |
Reason of the request (optional) |
Body Example:
{
"requester": "Jon Snow",
"requestReason": "The more you give a king, the more he wants."
}OK (201) Response Example:
{
"data": {
"_id": "67168be38ba437276aeed68b"
}
}- Returns the records stored in the database, along with additional statistics.
OK (200) Response Example:
{
"data": {
"docs": [
{
"_id": "67168be38ba437276aeed68b",
"requester": "Jon Snow",
"requestReason": "The more you give a king, the more he wants.",
"externalId": 106,
"name": "Dr. Schmidt",
"species": "Human",
"gender": "Male",
"image": "https://rickandmortyapi.com/api/character/avatar/106.jpeg",
"episodes": ["https://rickandmortyapi.com/api/episode/13"],
"url": "https://rickandmortyapi.com/api/character/106",
"createdAt": "2024-10-21T17:14:11.478Z",
"updatedAt": "2024-10-21T17:14:11.478Z"
}
],
"stats": {
"totalDocs": 1,
"requesters": 1,
"characters": 1,
"maleGender": 1,
"femaleGender": 0,
"genderless": 0,
"unknownGender": 0,
"species": 1,
"episodes": 1
}
}
}- Generates a downloadable CSV file with the data stored in the database.
- Additional endpoint as a "health check" utility to verify correct deployment and active status of the server.
OK (200) Response Example:
{
"data": "OK"
}