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
).
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 simpledb.json
file.
They talk to each other using HTTP requests, which is a standard way for services to communicate on the web.
This version is great for understanding the basics of how the two services run and communicate on your local machine without any containerization.
- Node.js installed on your computer.
-
Check out the
v1.0.0
tag:git checkout v1.0.0
-
Start the Backend (
name-service
): Open your first terminal window, navigate to thename-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
. -
Start the Frontend (
frontend-service
): Open a second terminal window, navigate to thefrontend-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
. -
View the Application: Open your web browser and go to http://localhost:3000.
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.
- Docker Desktop installed and running on your computer.
-
Check out the
v2.0.0
tag:git checkout v2.0.0
-
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 yourDockerfile
s. You only need to use it the first time or after you make code changes.
-
View the Application: Open your web browser and go to http://localhost:3000.
-
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
-
Loading the Page: Your browser asks the
frontend-service
(atlocalhost:3000
) for the webpage. The service sends backindex.html
,style.css
, andscript.js
. -
Fetching the Names: As soon as the page loads,
script.js
immediately makes an HTTP GET request to thename-service
's API endpoint (e.g.,http://localhost:3001/names
). -
Backend Responds: The
name-service
receives this request, reads thedb.json
file, and sends the list of names back to the frontend as a JSON response. -
Displaying the Data: The
script.js
file receives the list of names and dynamically builds the HTML table to display them on the page. -
Adding a Name: When you add a name, the frontend sends a POST request to the
name-service
. The backend adds the new name todb.json
, and the frontend re-fetches the list to update the table. -
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 updatesdb.json
, and the frontend refreshes the list.