Building a Memory Sharing API with Node.js, MongoDB, and Docker
Tags: docker, mongodb, node, javascript
Published: 2023-01-14 21:31:31

# Introduction

The Memory App is a web application that enables users to create and share memories with others. The app is built using JavaScript language, MongoDB for the database, and follows the MVC (Model-View-Controller) architecture. In this blog post, we will discuss the project structure of the Memory App, and explain how to install and run the app via Docker.

## Project Structure

The Memory App has a well-organized project structure that includes several important files and directories. The following is a brief description of each directory:

- The `app.js` file serves as the entry point for the application.
- The `config` directory contains the `db.js` file, which sets up the database connection.
- The `controllers` directory includes files that handle the logic for different routes, such as `commentController.js` and `memoryController.js`.
- The `middleware` directory includes files that handle authentication and error handling, such as `authMiddleware.js` and `errorMiddleware.js`.
- The `models` directory includes files that define the schema for different data types, such as `memoryModel.js` and `userModel.js`.
- The `routes` directory includes files that define the various API endpoints for the app, such as `memoryRoutes.js` and `userRoutes.js`.
- The `services` directory includes files that handle the business logic for different features, such as `memoryServices.js` and `userServices.js`.
- The `utils` directory includes files that handle utility functions, such as `uploadImage.js` and `sendMail.js`.
- The `validators` directory includes files that handle input validation, such as `memoryValidator.js` and `userValidator.js`.

## Docker Installation

Docker is a containerization platform that enables developers to package their applications and dependencies together in a portable container. This allows for consistent and reproducible environments across different development, testing, and production environments. The Memory App can be easily installed and run using Docker.

Before installing the Memory App, make sure that Docker is installed on your system. You can download the Docker Desktop for Windows or Mac, or install the Docker Engine for Linux.

Once Docker is installed, navigate to the root directory of the Memory App in your terminal. The app is configured to be run using Docker Compose, which is a tool for defining and running multi-container Docker applications. The app's Docker Compose configuration is defined in the file `docker-compose.yml`.

To start the app, run the command `docker-compose up` in the terminal. This command will start the app's containers and make the app available at `http://localhost:5000/api/memory`.

To stop the app and remove the containers, run the command `docker-compose down`.

To run the app in production mode, use the command `docker-compose -f docker-compose.yml -f docker-compose.prod.yml up`. This will make the app available at `http://localhost/api/memory`.

You can pass an additional flag `-d` to the command to keep the containers running in the background.

It is also possible to override the default settings of the compose file using a separate override file by running the command `docker-compose -f docker-compose.yml -f docker-compose.override.yml up`. `docker-compose up` automatically applies this if we name the file `docker-compose.override.yml`.

In production mode, the environment variables need to be set in the production environment. We could create a separate `.env` file for production and provide it inside the `docker-compose.prod` file. We could also containerize a MongoDB database.

{% embed https://github.com/idrsdev/memoryapp %}