lundi 16 août 2021

Is this approach good in project design node js?

In my API projects - I use the Controller -> Service -> Model pattern. In simple projects enough, describe all the functions in one file.
user.controller.js

const ApiError = require('../error/ApiError');
const yup = require('yup');
...

exports.login = (req, res, next) => {...}
exports.signup = (req, res, next) => {...}
exports.getInfo = (req, res, next) => {...}
exports.changePassword = (req, res, next) => {...}

But when the project develops, the code grows accordingly, and it becomes inconvenient to store everything in 1 file. For example, when a file consists of 500 lines of code and you need to change somewhere 1 line of code, you have to scroll down, which is very inconvenient. I decided, what if each function is divided into sub files and in the end everything is included in index.js

controllers/user/
- index.js (Единая точка входа для всех функций);
- login.js;
- signup.js
...

and it turns out something like
controllers/user/login.js

const ApiError = require('../error/ApiError');
const yup = require('yup');
...

module.exports = (req, res, next) => {...}

controllers/user/index.js

const login = require('./login');
const signup = require('./signup');
...

module.exports = {
 login,
 signup 
};

Now it is more convenient to edit functions, and they are all stored at one point.
Do you think my approach to design is normal or is there something similar?

Aucun commentaire:

Enregistrer un commentaire