NodeJS Code Structure
This project is a Fastify-based application that integrates with Azure Service Bus for message processing. It follows a modular and scalable structure to ensure maintainability.
Project Structure
π node-project
βββ π .docker # Dockerfiles and docker-compose.yaml
βββ π database # Temporary location for our database schema scripts
βββ π dist # TypeScript compiler results (not committed)
βββ π reports # Code quality reports (currently only coverage reports)
βββ π src # All of the implementation source code
β βββ π config # Configuration files (e.g., Azure Service Bus host settings)
β βββ π controllers # API request/response handlers and schema
β βββ π internals # Reusable code that could become a new node package
β βββ π routes # API route definitions
β βββ π services # Business logic (upload, copy file, send to extractor, etcβ¦)
β βββ π subscribers # Azure Service Bus message subscribers
β βββ π types # Type definitions that get re-used in different parts of the pp
β βββ π utils # Junk drawer (Ideally empty)
β βββ π index.ts # Initializes and starts the application
βββ π test # ALL automated tests
β βββ π unit # ALL Unit tests (ONLY Unit tests)
βββ π .env # Environment variables FOR CONFIGURATION (not committed)
βββ π .npmrc # Configuration that allows us to use our private npm registry
βββ π .gitignore # Files and folders for git to ignore
βββ π eslint.config.ts # ESLint rules configuration
βββ π package.json # Dependencies and scripts
βββ π package-lock.json # Used by npm to fetch consistent dependencies (not committed)
βββ π pull_request_template.md # Pull request template with the checklist in it
βββ π README.md # Information about this project
βββ π tsconfig.json # TypeScript compiler configuration
βββ π vitest.config.ts # Test configuration for vitest framework
π config - Configuration
Stores configuration files such as Azure Service Bus credentials.
Not TypeScript files unless that is the config file format that is required.
π config/appConfig.json
π src - All of the TypeScript implementation files for our microservice
π src/controllers - API request/response handlers and schema
π src/controllers/message.ts
π src/internals - Reusable code that could become a new node package
As we discover functionality that we want to reuse we can create a subfolder here to hold all of the related functionality. For example, if we begin by subscribing to a single Service Bus topic but later realize we need to subscribe to more than one we could create a subfolder here called something like βserviceBusSubscribeβ and we could put a general purpose function that can take a topic name and a function to handle each message so that when we need to create a subscriber for a particular topic we can do it the same way each time. This repetition might be within the current project OR across multiple microservices.
There could be other reasons to create a separate node package for some of our code. One example is that we have created a feature that takes a long time to run the test suite for and which we change very seldom. If we put that code and the related tests into a separate package we can pay the price of running those tests only when we need to change that feature. So if that test suite took 2 minutes to run, instead of waiting 2 minutes in our microservice for each test run, we can save 2 minutes for each test run in the microservice and only need that 2 minute test suite when we are changing that package.
π src/routes - All route assignments for our REST API routes
π src/routes/customeRoute.ts
π src/plugins - Fastify Plugins
(PLEASE DO NOT USE THIS FOLDER UNTIL WE TALK ABOUT IT)
Handles database connections and Azure Service Bus setup.
π src/plugins/azureServiceBus.ts
π src/services - Business Logic
Contains logic to send messages to Azure Service Bus.
π src/services/azureServiceBusService.ts
π src/subscribers - Message Consumers
Consumes messages from Azure Service Bus and processes them. If we find a source of messages other than the Azure Service Bus, it seems like handlers for those messages belong here too.
π src/subscribers/messageQueueSubscriber.ts
π src/index.ts - Framework Initialization and Entry Point
Initializes DevFramework, registers plugins, enables features, and starts the server.
π test/unit - ALL Unit tests (ONLY Unit tests)
The folder structure in the test/unit folder should mirror the folder structure in the src folder. In other words, if there is a src/services/awesomeService.ts then the unit tests would be in test/unit/services/awesomeService.test.ts.
π .env - Environment Variables
(PLEASE HELP MAKE SURE THIS DOES NOT GET COMMITED TO THE REPO)
π package.json - NodeJS Package Configuration & Dependencies
This example file assumes we are building a service with a REST API using the CLA DevFramework libraries using vitest as a test-framework.