Why do we need to separate the concerns?
Imagine if you have to do your groceries inside a bank, wouldn’t that be chaotic? Or imagine if your groceries aren’t categorized where you’ll have to scour through the entire cupboard just to find the ingredient you need?
Likewise, programming works the same way. We separate our concerns for code readability and code maintainability. How so? Let’s first understand what a model, controller and routes is.
Model
Model is where your structure resides, it is where your data blueprint is. It contains the properties as well as the data type that we’re expecting from the user. This is where the schema and the model should be, like so:
const mongoose = require('mongoose');
const taskSchema = new mongoose.Schema({
name: String,
status: String,
});
module.exports = mongoose.model('Task', taskSchema);
Controller
Controller is where our business logic resides. This is where the brain of the program is. Previously, we’d put all the business logic inside the route like so:
app.post('/items', (req, res) => {
let newItems = {
name: req.body.name,
price: req.body.price,
isActive: req.body.isActive,
};
items.push(newItems);
res.send(items);
});
Our business logic can be quite convoluted if there are hundreds or thousands of them. Hence, we separate it in a different file called “Controller.js” under a folder called “controllers” where all our controllers will reside.
On that note, please bear in mind that a controller goes side by side with the route.
Route
Route is where the routes reside. This is where the **endpoints **should be defined. As mentioned above, the route, work side by side with the controller, like so:
const express = require('express');
const router = express.Router();
const taskControllers = require('../controllers/taskControllers');
console.log(taskControllers);
router.post('/', taskControllers.createTaskController);
module.exports = router;
Mongoose Router
Mongoose router allows us to use HTTP methods without having direct access to our server. What do I mean by that? In order to have access to the HTTP methods, we need to have access to the server which is defined by calling the server instance declared using the variable **app **and directly access the HTTP methods directly like so (see line number 3):
const express = require('express');
const app = express();
app.get('/items/getSingleItem/:item', (req, res) => {
res.send(items[+req.params.item]);
});
However, since the route file does NOT have access to the HTTP methods as it is NOT a server, we need a way to access it in order to define the routes and its function that needs to be performed.
const express = require('express');
const router = express.Router();
//Router method - allows access to HTTP methods
const taskControllers = require('../controllers/taskControllers');
console.log(taskControllers);
//create task route. At the moment, we don't have direct access
to the server, hence, we NEED to use router in order to have access
to the HTTP methods
router.post('/', taskControllers.createTaskController);
module.exports = router;
Top comments (0)