# Backend Code Overview
[Youtube Video](https://www.youtube.com/watch?v=hsF-PiOCW8o&list=PLG3lylzOg8Fjf2oqcP2faN8edjOmwNyyF)

# What it does
The backend exposes an API for the frontend to consume. It handles interactions with Mongo, some data processing, and also serves an api frontend with Swagger.

# Design
![architecture_reference](../documents/pictures/simple_class_diagram.png)

The lower section of the diagram above refers to the Backend.  
As stated in the video linked above, the code is broken down into layers for different responsibilities.
These layers are broken down below in the Directories section. The diagram above can be used as a map to see where each directory fits into the request flow hierarchy.

# Base
The root of the backend comes from the [Express No Stress](https://github.com/cdimascio/generator-express-no-stress) generator.  

# Directories

## Server (Application Code)
`index.js` Application insertion point (listens on specified port)  
`routes.js` Routes requests with Express to endpoint routers

### api
Contains api logic

#### controllers
Contains a controller and router per object type.  
**Router** Recieves request from root `routes.js` send request to the correct controller function.  
**Controller** provides api logic/response, calls the downstream service to perform actions

#### services
Contains a **Service** per object type.  
These services take the incoming json data and call out to the model to lookup/modify/create/delete.

#### models
Contains a **Model** per object type.
These models are `Mongoose.model` objects: https://mongoosejs.com/docs/models.html  
These models are easy to use facades for interacting with MongoDB

### common
`DB.js` contains URI for Mongodb (points to docker setup)  
`env.js` imports `dotenv` and uses it to pull the data from the `.env` file below  
`logger.js` Sets up pino logger (from No-Stress)
`server.js` Sets up Express and Swagger (partially from No-Stress)

#### swagger
Contains configuration/code specific to swagger. [swagger-js](https://github.com/swagger-api/swagger-js)
`api.yaml` This file is **important** api requests are checked against it. Any incoming request needs to conform to the Swagger definition!
`index.js` Swagger configuration/middleware setup

## Public
Swagger files (No need to edit/modify)

## Test
Test Code

## Special files in Root
`.env` contains configuration  
`mongo-init.js` code to bootstrap mongodb setup  
`docker-compose.yml` docker compose definition to create mongodb and mongo-express containers  
`no_stress_express_readme.md` auto-generated readme from [No Stress Express](https://github.com/cdimascio/generator-express-no-stress)
