How to Better Structure Your Next Nodejs Project? The Modular Monolith Approach

Listen to this Post

πŸ‘‰ Read the full article here

You Should Know:

Here are some practical commands and code snippets to help you implement a Modular Monolith architecture in your Node.js project:

1. Setting Up a Basic Node.js Project


<h1>Initialize a new Node.js project</h1>

npm init -y

<h1>Install Express.js for building the server</h1>

npm install express

<h1>Install Nodemon for development (auto-restart on changes)</h1>

npm install --save-dev nodemon 

#### **2. Structuring Your Modular Monolith**

Create a folder structure like this:

[plaintext]
src/
β”‚
β”œβ”€β”€ modules/
β”‚ β”œβ”€β”€ user/
β”‚ β”‚ β”œβ”€β”€ controllers/
β”‚ β”‚ β”œβ”€β”€ services/
β”‚ β”‚ β”œβ”€β”€ routes/
β”‚ β”‚ └── tests/
β”‚ β”œβ”€β”€ product/
β”‚ β”‚ β”œβ”€β”€ controllers/
β”‚ β”‚ β”œβ”€β”€ services/
β”‚ β”‚ β”œβ”€β”€ routes/
β”‚ β”‚ └── tests/
β”‚
β”œβ”€β”€ config/
β”‚ └── db.js
β”œβ”€β”€ app.js
└── server.js
[/plaintext]

#### **3. Example Module: User Module**

**`src/modules/user/routes/userRoutes.js`**

const express = require('express'); 
const router = express.Router(); 
const userController = require('../controllers/userController');

router.get('/users', userController.getAllUsers); 
router.post('/users', userController.createUser);

module.exports = router; 

**`src/modules/user/controllers/userController.js`**

const userService = require('../services/userService');

const getAllUsers = async (req, res) => { 
const users = await userService.getAllUsers(); 
res.json(users); 
};

const createUser = async (req, res) => { 
const newUser = await userService.createUser(req.body); 
res.status(201).json(newUser); 
};

module.exports = { getAllUsers, createUser }; 

#### **4. Connecting Modules in `app.js`**

const express = require('express'); 
const userRoutes = require('./modules/user/routes/userRoutes'); 
const productRoutes = require('./modules/product/routes/productRoutes');

const app = express();

app.use(express.json()); 
app.use('/api', userRoutes); 
app.use('/api', productRoutes);

module.exports = app; 

#### **5. Running the Server**

**`server.js`**

const app = require('./app'); 
const PORT = process.env.PORT || 3000;

app.listen(PORT, () => { 
console.log(<code>Server running on port ${PORT}</code>); 
}); 

Run the server:

nodemon server.js 

### **What Undercode Say:**

The Modular Monolith approach is a powerful way to structure your Node.js projects, especially when starting out. It allows you to maintain a clean separation of concerns while avoiding the complexity of microservices. By organizing your code into modules, you can easily scale and refactor your application as it grows.

Here are some additional Linux and IT-related commands to enhance your development workflow:

#### **Linux Commands for Developers**


<h1>Monitor system resources</h1>

htop

<h1>Search for files in a directory</h1>

find /path/to/dir -name "*.js"

<h1>Check running processes</h1>

ps aux | grep node

<h1>Compress a folder into a tar.gz file</h1>

tar -czvf archive.tar.gz /path/to/folder 

#### **Windows Commands for Developers**


<h1>Check active network connections</h1>

netstat -an

<h1>List all running processes</h1>

tasklist

<h1>Kill a process by PID</h1>

taskkill /PID <process_id> /F 

For more advanced Node.js architecture tips, refer to the original article: How to Better Structure Your Next Node.js Project?.

References:

Reported By: Petarivanovv9 Ive – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass βœ…Featured Image