» Node.js: Building Event-Driven Microservices with Kafka » 3. Consumer: Trend Service » 3.1 Service Design

Service Design

Event Queue

As shown in this diagram, the Trend Service consumes “search book“ events from the queue, analyzes them and lists the “trendy“ books to the users.

Our trend service should have at least 2 components:

Trend Service Arch

  • Consumer: It consumes and analyzes events from the queue.
  • API Server: It lists trends to the users.

We‘ll have 3 services in this tutorial, but we don‘t have to make 3 repositories since they're all written in Node.js. Let's try the monorepo.

Monorepo

A monorepo, short for "monolithic repository," is a software development approach where multiple projects or components of a system are stored within a single repository.

We need to structure the project like this:

.
├── LICENSE
├── README.md
├── package-lock.json
├── package.json
├── src
│   ├── domain
│   │   └── model
│   │       ├── book.ts
│   │       └── index.ts
│   ├── recommendation
│   │   └── app.ts
│   ├── trend
│   │   └── app.ts
│   └── web
│       ├── adapter
│       │   ├── index.ts
│       │   ├── router.ts
│       │   └── templates
│       │       └── index.handlebars
│       ├── app.ts
│       ├── application
│       │   ├── executor
│       │   │   ├── book_operator.ts
│       │   │   └── index.ts
│       │   ├── index.ts
│       │   └── wire_helper.ts
│       ├── config.json
│       ├── domain
│       │   └── gateway
│       │       ├── book_manager.ts
│       │       └── index.ts
│       └── infrastructure
│           ├── config
│           │   ├── config.ts
│           │   └── index.ts
│           ├── database
│           │   ├── index.ts
│           │   └── mysql.ts
│           └── mq
│               ├── index.ts
│               └── kafka.ts
└── tsconfig.json

Restructure the folders

  • Move src/ sub-folders adapter/, application/, domain/gateway/ and infrastructure/ into src/web/.
  • Move app.ts and config.json files into src/web/.
  • Update import paths in related files.
  • Modify file path config values.

Tune src/web/app.ts:

- const config_filename = "config.json";
+ const config_filename = "src/web/config.json";

Tune src/web/config.json:

-     "templates_dir": "src/adapter/templates"
+     "templates_dir": "src/web/adapter/templates"

Tune package.json:

- "dev": "ts-node src/app.ts",
+ "dev-web": "ts-node src/web/app.ts",