Microservices Project Structure and Setup

Build four independent Express services for users, orders, inventory, and simulated payments, understand project structure and shared code boundaries, and verify independent execution.

🎯 Introduction

The previous article divided the store into user, order, inventory, and simulated payment services. Now we need runnable projects for those responsibilities: each service has its own entry point and start command, and stopping one service does not stop the other processes. This article builds the project skeleton used throughout the course and explains where shared code belongs. You have already learned Node.js and HTTP, so we will not revisit routing or framework fundamentals.


🧱 Organizing a Microservices Project

We will keep all four services in one Git repository to make them easier to manage while learning. This is a monorepo. It describes where code lives, without requiring services to start or ship together.

microservices/
├── services/
│   ├── users/
│   │   ├── package.json
│   │   └── src/
│   │       └── app.mjs
│   ├── orders/
│   │   ├── package.json
│   │   └── src/
│   │       └── app.mjs
│   ├── inventory/
│   │   ├── package.json
│   │   └── src/
│   │       └── app.mjs
│   └── payments/
│       ├── package.json
│       └── src/
│           └── app.mjs
└── README.md

Each directory has a purpose:

  • services/: contains the business services.
  • Each service's package.json: defines its name, Express dependency, and start command.
  • src/app.mjs: the service entry point. Add business files to each service's src/ later; there is no need for a complex directory structure upfront.
Separate repositories, separate processes, and independent deployment are different concepts. Here we verify process independence by running the services. Independent deployment also requires each service to have its own build, configuration, and release process, which we will cover later.

🛠 Creating Four Independent Services

The following commands work in macOS or Linux terminals. The examples use Node.js 22.x and Express 5.1.0 with ESM modules. We will not repeat Express routing basics.

1. Create the directories

mkdir -p microservices/services/{users,orders,inventory,payments}/src
cd microservices

2. Create a separate Node.js project for each service

First, write the following to services/users/package.json:

{
    "name": "users-service",
    "private": true,
    "type": "module",
    "scripts": {
        "start": "node src/app.mjs"
    },
    "dependencies": {
        "express": "5.1.0"
    }
}

Use the same structure for the other three services, changing name to orders-service, inventory-service, and payments-service, respectively. Each service can then manage its own dependencies and start command.

Install dependencies for each service. Run all commands from the microservices/ project root. Each command installs only the dependencies declared in that service's package.json:

npm install --prefix services/users
npm install --prefix services/orders
npm install --prefix services/inventory
npm install --prefix services/payments

If you are already in a service directory, you can run npm install there instead. You do not need to install Express again at the project root.

Each service will have its own package-lock.json and node_modules/. Keep the lockfiles in Git and add node_modules/ to the root .gitignore to exclude dependency directories. Running npm install requires access to the npm registry.

3. Write the Express entry points

Create each service's src/app.mjs below. Switch tabs and write the complete code to the corresponding file; you do not need to copy a file and then edit it manually.

services/users/src/app.mjs:

import express from 'express';

const app = express();
const serviceName = 'users';
const port = 3001;

app.get('/health', (req, res) => {
    res.json({ service: serviceName, status: 'ok' });
});

app.listen(port, () => {
    console.log(`${serviceName} listening on ${port}`);
});

All four Express services expose /health to check whether they respond. For now, this endpoint only verifies that the HTTP entry point is reachable; it does not establish that dependencies such as databases or message queues are healthy. We have created entry points without implementing interservice requests. The next article explains how orders calls inventory.


⚙️ Keeping Services Independent

Each service has its own package.json, src/app.mjs, and listening port. Even in one repository, changing order service code does not require changing the other three entry points.

ServiceListening portBusiness responsibility
User service3001User profiles and account status
Order service3002Create orders and maintain order status
Inventory service3003Query, reserve, and release stock
Simulated payment service3004Record simulated payment outcomes

Fixed ports make local startup and verification easier. Each service can later add its own business files and dependencies without requiring the others to use an identical structure.


🧩 Managing Shared Code

With four services in one repository, extracting all repeated code can be tempting. But keeping code together does not mean business rules should be shared.

A date formatter simply converts times to a consistent format and is usually safe to share. Whether an order can be canceled, however, depends on order state and belongs to the order service. If inventory imports an internal order cancellation function, changes to order rules could force inventory changes or redeployment.

Consider sharingDo not share directly
Stable date formatting utilitiesInternal order status decision logic
General log formats without business rulesInternal inventory SQL operations
Stable general-purpose utilitiesInternal payment status transition logic

There is no need to create a shared utility package here. Consider extracting a separate package only when multiple services actually reuse a stable, general-purpose capability. Expose business capabilities through agreed APIs or events instead of importing another service's internal business files.


✅ Starting Services and Verifying Independence

Open four terminals. Run every command from the microservices/ project root:

npm start --prefix services/users
npm start --prefix services/orders
npm start --prefix services/inventory
npm start --prefix services/payments

Run each command in a different terminal, with microservices/ as the working directory in all four.

Open a fifth terminal and check the four Express services:

curl http://localhost:3001/health
curl http://localhost:3002/health
curl http://localhost:3003/health
curl http://localhost:3004/health

You should see responses like this, one line per command:

{"service":"users","status":"ok"}

The other three responses have service values of orders, inventory, and payments.

Finally, press Ctrl + C in the order service terminal to stop it. Request these endpoints again:

curl http://localhost:3002/health
curl http://localhost:3003/health

The order connection should fail, while inventory should still respond normally. This demonstrates that they are independent processes. It does not yet prove that business operations remain available when a dependency fails. Later discussions of timeouts, retries, and fault tolerance address that problem.


❓ Knowledge Check

Q1: Can services in one repository deploy separately?

Yes. A Git repository determines code organization. Deployment depends on independent build, configuration, and release capabilities. Here we establish independent execution first.

Q2: Must every service have its own package.json?

No. A monorepo can also manage dependencies through workspaces. This course uses separate package.json files to make Express dependencies, boundaries, and startup commands easier to see.

Q3: Why not simply import the inventory function that orders needs?

That would couple orders to inventory's internal implementation. Changes to inventory rules could affect both projects. Collaborating through inventory's public API makes responsibility boundaries easier to maintain.


🧪 Exercise

Verify that the four Express services really run independently:

  1. Start users, orders, inventory, and simulated payments separately.
  2. Request each /health endpoint and verify its service name.
  3. Stop orders, then request the order and inventory /health endpoints again.
  4. Restart orders and verify that it responds again.

Acceptance criteria: All four services start individually. After orders stops, its endpoint is unreachable while inventory still responds. Restarting orders restores its endpoint.


🎉 Congratulations on completing this article! You now have four Express services that start separately. Next, we will make them collaborate.