DEV Community

Cover image for Building a Memory Sharing API with Node.js, MongoDB, and Docker
Malik-Idrees
Malik-Idrees

Posted on • Updated on

Building a Memory Sharing API with Node.js, MongoDB, and Docker

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.

GitHub logo idrsdev / memoryapp

API for a social media application where user can post, share, view, comment and like memories.

Table of Contents

  1. Installation
    1. Locally
      1. Install Dependencies
      2. Run
    2. Docker
  2. API Docs
    1. Open Endpoints
    2. Endpoints that require Authentication
    3. Memory related
    4. User related
    5. Comment related
    6. Reset Password
    7. Upload Image
  3. Project Structure

Installation

Locally

Install Dependencies

$ npm install

$ cp .env.example .env

set these environment variables

Run
npm run dev (:5000)

Docker

$ docker compose up
Available at http://localhost:5000/api/memory

To stop and remove containers
$ docker compose down

To run in production mode
$ docker compose -f docker-compose.yml -f docker-compose.prod.yml up
Available at http://localhost/api/memory

You can pass an additional flag -d to keep it running in background

API Docs

BaseURL : http://localhost:5000/api

Open Endpoints

Open endpoints require no Authentication.

  • Login POST /user/login/

Endpoints that require Authentication

Closed endpoints require a valid Token to be included in the header of the request. A Token can be acquired from the Login view above.

Memory related

Endpoints for viewing and manipulating the Memories.

  • Create Memory:

Top comments (0)