The secret to building a large project that is easy to maintain and performs better is to separate files into components that can change independently without affecting other components
An important goal of clean architecture is to provide developers with a way to organize code in such a way that it encapsulates the business logic but keeps it separate from the delivery mechanism.
In this article, we will discuss Clean Architecture in Node.js, and explore how to implement it in your Node.js projects with code samples and how to apply the concepts to them.
A bad Practice code.
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = 5000; // Choose a suitable port number
app.use(express.json()); // For parsing JSON data
// Define your API endpoints here
app.get('/', (req, res) => {
res.send("Hello from express ")
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
In this code section, we fix all the code bases such as routing, controller
Detecting and Solving Problems
When developing Node.js applications, we often face some problems such as code duplication, and an unorganized codebase. In the next section, we use a simple example of a Node.js application that retrieves data from a database and returns it as a JSON response to solve these problems. Clean Architecture provides solutions.
Now Separation of code.
index.js file
const express = require('express')
const usersRouter = require('./routes/users')
const app = express()
const PORT = 5000;
app.use(express.json());
//Middleware
app.use('/users', usersRouter)
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Routes
The /routes folder is where you can organize all of your different REST endpoints declarations. The file below exposes all available endpoints related to the Orders Entity:
routes/users.js
const express = require('express');
const userController = require('../controller/contoller')
const router = express.Router();
router.get('/',userController.getUsers)
module.exports = router
Let’s create a folder inside the project folder, let’s call it controllers
, inside this folder
Controller functions to get the requested data from the models, create an HTML page displaying the data, and return it to the user to view in the browser.
Controller.js
const database = require('../database/user');
const getUsers = (req,res)=>{
try {
const user = database.getAllusers();
res.send({status:"OK",data:user});
}
catch (error) {
res.status(500).send("Internal Server Error");
}
}
module.exports = { getUsers,}
Database
Actual data come from here.
const DB = require('./DB.json')
const getAllusers = () =>{
return DB
}
module.exports = {getAllusers}
//NoTe - I am using DB.json File which contain data.
if you are dealing with data Schemas then you can create the file inside your database folder so it is easy to maintain schemas and related things🤗
Folder Structure
Thank you for reading my content. Be sure to follow and comment on what you want me to write about next
🤓