Skip to content

API RESTFul implementation to put into practice concepts like dockerization, e2e tests, swagger documentation, and others.

Notifications You must be signed in to change notification settings

christianjtr/express-api-mongodb-docker-jest-supertest

Repository files navigation

JS API RESTFul test

This is a conceptual test that puts into practice the following core technologies:

🎯 Goals

  • Build an API RESTFul based on the JS ecosystem following the given statements.
  • Document the API.
  • Dockerize the implementation.
  • Add some integration test to check if everything is ok.

πŸ’‘ Suggestions you may dig in

  • Adopt TypeScript.
  • Add unit tests.
  • Protect routes (Authorization and Authentication).
  • Prepare the implementation to be deployed.
  • Adopt an event-driven approach or caching strategy.

Assestment

Write an API that will be queried by two services: X and Y services.

The X service will need to keep in sync with the list of Couriers in the platform and their max capacity (in liters).

curl -X POST http://localhost:3000/couriers --data '
{
    "max_capacity": 45
}'

The Y service will need to query this API to determine which couriers have available space.

curl -X GET http://localhost:3000/couriers/lookup --data '
{
    "capacity_required": 45
}'

Write the API that will allow adding, removing, and updating couriers' capacities, and that will let lookup a list of couriers whose capacity is greater or equal to the one required.

How to run the project

The project requires Node.js, MongoDB, and Docker to run.

Clone the repository:

git clone https://github.com/christianjtr/express-api-mongodb-docker-jest-supertest.git

Environment variables:

Env. Var. Value
NODE_ENV development or test
SERVER_PORT 3001
API_VERSION v1
SEED_MOCKED_DATA true or false
MONGODB_HOST localhost
MONGODB_PORT 27017
MONGODB_DB <YOUR_DATABASE_NAME>

You can modify the variables by your preferences at .env and .env.test files

Start the application

Initialize Docker, pull down the images, create, and build the containers.

npm run app:start
Stop the application

Shut down the application.

npm run app:stop
Start server application (Non-dockerized version)

To run the application without using Docker, you will need to install MongoDB Community Version: 4.4 in your OS System.

npm run server:start
Run test suites

There are two simple integration tests as an example.

npm test

Important

Technologies:

API Endpoints

Documentation was generated using Swagger along with a .yml file having the API specs.

Parameters:

Noticed that you can change these parameters in .env files

Param Value
host localhost
port 3001
version v1
Base URI:
BaseURI: http://localhost:3001/api/v1/{resource}

API Docs

Swagger API Docs

URI: {BaseURI}/docs

Health checks

Example cURL
$ curl --location 'http://localhost:3001/api/v1/health-checks/alive'

Couriers

Basic CRUD operations

URI: {BaseURI}/couriers
Get all couriers
Example cURL
$ curl --location 'http://localhost:3001/api/v1/couriers'
Get courier by ID
Example cURL
$ curl --location 'http://localhost:3001/api/v1/couriers/65b93d111efb464a86c6d109'
Create courier
Example cURL
$ curl --location 'http://localhost:3001/api/v1/couriers' \
--header 'Content-Type: application/json' \
--data '{
    "max_capacity": 100
}'
Update courier by ID
Example cURL
$ curl --location --request PUT 'http://localhost:3001/api/v1/couriers/65b93d4e1afd3c4aab06a8d5' \
--header 'Content-Type: application/json' \
--data '{
    "max_capacity": 50
}'
Delete courier by ID
Example cURL
$ curl --location --request DELETE 'http://localhost:3001/api/v1/couriers/65b93d92d1f6ff4ad25961c6'

Courier actions

URI: {BaseURI}/couriers/lookup/:capacity_required
Lookup

Request data from this API to determine which couriers have available space. (Greater or equals to the requested capacity)

Example cURL
$ curl --location 'http://localhost:3001/api/v1/couriers/lookup/700'

Technologies

ExpressJS, MongoDB, Docker, Jest, SupertTest.