This is a conceptual test that puts into practice the following core technologies:
- 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.
- Adopt TypeScript.
- Add unit tests.
- Protect routes (Authorization and Authentication).
- Prepare the implementation to be deployed.
- Adopt an event-driven approach or caching strategy.
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.
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
Initialize Docker
, pull down the images, create, and build the containers.
npm run app:start
Shut down the application.
npm run app:stop
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
There are two simple integration tests as an example.
npm test
Documentation was generated using Swagger along with a .yml
file having the API specs.
Noticed that you can change these parameters in .env files
Param | Value |
---|---|
host |
localhost |
port |
3001 |
version |
v1 |
BaseURI: http://localhost:3001/api/v1/{resource}
Swagger API Docs
URI: {BaseURI}/docs
$ curl --location 'http://localhost:3001/api/v1/health-checks/alive'
Basic CRUD operations
URI: {BaseURI}/couriers
$ curl --location 'http://localhost:3001/api/v1/couriers'
$ curl --location 'http://localhost:3001/api/v1/couriers/65b93d111efb464a86c6d109'
$ curl --location 'http://localhost:3001/api/v1/couriers' \
--header 'Content-Type: application/json' \
--data '{
"max_capacity": 100
}'
$ curl --location --request PUT 'http://localhost:3001/api/v1/couriers/65b93d4e1afd3c4aab06a8d5' \
--header 'Content-Type: application/json' \
--data '{
"max_capacity": 50
}'
$ curl --location --request DELETE 'http://localhost:3001/api/v1/couriers/65b93d92d1f6ff4ad25961c6'
URI: {BaseURI}/couriers/lookup/:capacity_required
Request data from this API to determine which couriers have available space. (Greater or equals to the requested capacity)
$ curl --location 'http://localhost:3001/api/v1/couriers/lookup/700'
ExpressJS, MongoDB, Docker, Jest, SupertTest.