Skip to content

Rahmat-JS/super-mini-micro-service-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Super minimal Microservice project with Pure Node.js

This project is a simple, educational demonstration of the microservice architecture. It's built with pure Node.js (no external frameworks like Express) to focus on the core concepts.

The repository is versioned using Git tags. You can run the original, manual version (v1.0.0) or the containerized Docker version (v2.0.0).

🏛️ Microservice Architecture Explained

In a traditional "monolithic" application, all the code for the user interface and the backend logic is in one large codebase. In a microservice architecture, we break that down into smaller, independent services. Think of it like a team of specialists instead of one person trying to do everything.

This project is split into two distinct services:

  • frontend-service: This is our "presentation specialist." Its only job is to create the webpage the user sees. It doesn't know how the data is stored; it only knows how to ask for it and display it.

  • name-service: This is our "data specialist." Its only job is to manage the list of names. It doesn't care how the data is displayed; it just provides a way to get, add, update, and delete names. It stores this data in a simple db.json file.

They talk to each other using HTTP requests, which is a standard way for services to communicate on the web.

✅ Version 1.0: Running Manually with Node.js

This version is great for understanding the basics of how the two services run and communicate on your local machine without any containerization.

Prerequisites

  • Node.js installed on your computer.

Instructions

  1. Check out the v1.0.0 tag:

    git checkout v1.0.0
    
    
  2. Start the Backend (name-service): Open your first terminal window, navigate to the name-service directory, and run its server.

    # In Terminal 1
    cd name-service
    node server.js
    
    

    You should see the message: Name service running on port 3001.

  3. Start the Frontend (frontend-service): Open a second terminal window, navigate to the frontend-service directory, and run its server.

    # In Terminal 2
    cd frontend-service
    node server.js
    
    

    You should see the message: Frontend service running on port 3000.

  4. View the Application: Open your web browser and go to http://localhost:3000.

🐳 Version 2.0: Running with Docker

This version uses Docker to package each service into a container. Docker Compose then runs both containers, connecting them so they can communicate. This is how modern applications are often developed and deployed because it ensures they run the same way everywhere.

Prerequisites

Instructions

  1. Check out the v2.0.0 tag:

    git checkout v2.0.0
    
    
  2. Build and Run the Containers: Navigate to the root directory of the project (the one containing the docker-compose.yml file) and run a single command:

    # In the project's root directory
    docker-compose up --build
    
    
    • --build: This flag tells Docker Compose to build the images from your Dockerfiles. You only need to use it the first time or after you make code changes.
  3. View the Application: Open your web browser and go to http://localhost:3000.

  4. Stopping the Application: To stop both containers, press Ctrl+C in the terminal where Docker Compose is running. Then, to clean up and remove the containers, run:

    docker-compose down
    
    

⚙️ How It Works (Step-by-Step Logic)

  1. Loading the Page: Your browser asks the frontend-service (at localhost:3000) for the webpage. The service sends back index.html, style.css, and script.js.

  2. Fetching the Names: As soon as the page loads, script.js immediately makes an HTTP GET request to the name-service's API endpoint (e.g., http://localhost:3001/names).

  3. Backend Responds: The name-service receives this request, reads the db.json file, and sends the list of names back to the frontend as a JSON response.

  4. Displaying the Data: The script.js file receives the list of names and dynamically builds the HTML table to display them on the page.

  5. Adding a Name: When you add a name, the frontend sends a POST request to the name-service. The backend adds the new name to db.json, and the frontend re-fetches the list to update the table.

  6. Editing or Deleting: When you edit or delete, the frontend sends a PUT or DELETE request to the name-service with the specific item's ID. The backend updates db.json, and the frontend refreshes the list.

About

Here I tried to write a simple but microservice project

Resources

License

Stars

Watchers

Forks