Skip to content

aa-avila/searchmas-api-node-express-docker

Repository files navigation

SearchMas - Technical Test: Node.js, Express, Docker

Author: Agustín Avila Humerez

Agustín on LinkedIn

About this project

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.

Dependencies

The main dependencies used in the project are the following:

Dependencies for production

Additional dependencies for development

NOTE: For a complete list of dependencies, see the package.json file.

Installation

Prerequisites

It is required to have installed:

Clone the repository

git clone https://github.com/aa-avila/searchmas-api-node-express-docker.git
cd searchmas-api-node-express-docker

Downloading and installing dependencies

yarn install

Configuration

For 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 .env

For 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.

Run the server locally

Running in development mode (without Docker)

Build the app for the first time:

yarn build

To start the server in development mode, use the following command:

yarn dev

This command allows you to reinitialize your application whenever changes are made to the code, which helps speed up development iteration.

Running in production mode (without Docker)

To start the server in production mode:

yarn start

This command performs a clean compilation of the code and then starts the server, assigning the value 'production' to the NODE_ENV environment variable.

Running in production mode (with Docker)

  1. Build the Docker image:

    docker build -t api-searchmas .

    As a result, an image named api-searchmas will be created.

  2. 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.

Tests

Run tests using the following command:

yarn test

API Endpoints

POST /api/external-data

  • 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"
  }
}

GET /api/data

  • 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
    }
  }
}

GET /api/export-csv

  • Generates a downloadable CSV file with the data stored in the database.

GET /health

  • Additional endpoint as a "health check" utility to verify correct deployment and active status of the server.

OK (200) Response Example:

{
  "data": "OK"
}

About

This project is an application developed with Express.js as part of a technical test for the Backend Developer (Node.js) position. It consists of a server that exposes a REST API, providing a series of endpoints that interact with a database and an external API, handling routes and middlewares.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors