gpt4 book ai didi

javascript - NodeJs MVC架构,这是通心粉代码吗?

转载 作者:行者123 更新时间:2023-11-30 19:34:59 25 4
gpt4 key购买 nike

我是一名初级 NodeJS 开发人员,目前在加密货币交易平台上工作。这是项目文件夹结构。

/app
/controllers
/user
/order
/settings
...
index.js
/middlewares
/models
/routes
user.js
order.js
/services
user.js
order.js
...
/views
index.js
/config
/migrations
/public
/utils
server.js
.env
...

现在,一开始有点不知所措,但后来,我在应用程序中四处走动变得很自在。
除了一个特定的文件! Controller 目录中的 index.js,这是它的设置方式。

const { readdirSync, lstatSync } = require('fs');
const { validationSchema } = require('../../utils/validator');

module.exports = readdirSync('app/controllers')
.filter(name => lstatSync(`app/controllers/${name}`).isDirectory())
.reduce((controllersAccumulator, dir) => Object.assign(
controllersAccumulator,
{
[`${dir}Controller`]: readdirSync(`app/controllers/${dir}`)
.map(fileName => require(`./${dir}/${fileName}`))
.reduce((accum, controllerFile) => Object.assign(
accum,
Object.keys(controllerFile).reduce(validationSchema.bind(null, dir, controllerFile), {}),
), {}),
},
), {});

我不得不承认,这一直让我害怕,只是看看它!所以简单来说,它的作用是将路由请求映射到 Controller 目录中的处理程序。

例如,如果用户想发一个post请求注册一个新账号:路由路径会是这样的:

// in the routes dir, user.js 
const { userCOntroller } = require('../controllers/');

router.post('/registration', userController.registration);


// in the /controllers, then /user, there will be a registration.js that includes:
const UserService = require('../../services/user');

exports.registration = async (req, res) => await UserService.create(req.body);


//then in the /services directory, user.js
...
class UserService {
static create(body) { ... }
...
}

module.exports = UserService

所以我仍然无法理解的是,我们是如何在最后一个片段的用户路由中导入 userController 的?所以这就是 /controllers 中的 index.js 文件生成的内容!
当我问团队中的资深人士时,他们说,是的,它很难阅读,但它的代码更少。好吧,好吧:\

所以,可以做些什么来使这个文件更具可读性,换句话说,有没有办法重构它?提前致谢!

最佳答案

解释第一个代码片段的最简单方法是重写它并添加适当的注释:

//Get all file names inside the controllers directory
const fileList = readdirSync('app/controllers');
//Get only directories from the file list
const onlyDirectories = fileList.filter(name => lstatSync(`app/controllers/${name}`).isDirectory());
//Create an empty object that will be exported from this module. Properties of this object will be assigned in the "for of" loop
const objectToExport = {};
//Import all files from the given directory and assign them to objectToExport
for (let directoryName of onlyDirectories){
//First get all file names
const controllerFiles = readdirSync(`app/controllers/${directoryName}`);
//Then require each of these files
const controllerModules = controllerFiles.map(fileName => require(`./${directoryName}/${fileName}`));
//Assign the imported modules to `objectToExport`
controllerModules.forEach((controllerModule) => {
//Validate each module and assign it only if it passes validation
if (validationSchema(directoryName, controllerModule)){
objectToExport[`${directoryName}Controller`] = controllerModule;
}
});
}

module.exports = objectToExport;

下面我将根据评论回答您的后续问题。结果对象现在看起来像这样:

{
userController: {
registration: [Function],
...
},
orderController: {
...
},
...
}

注册函数最终在 userController 属性中,因为它是由 registration.jsexported,然后 imported 通过我的第一个代码片段中的 require 语句。为了在其他文件中直接使用这个函数,你必须按以下方式解构它:

const { userController: {registration} } = require('../controllers/');
//now you can use it like this:
router.post('/registration', registration);

你的最后一个问题是关于req.body。如您所见,该函数有两个参数:reqres:

exports.registration = async (req, res) => await UserService.create(req.body);

然后它作为中间件传递给您的路由器。我假设您使用的是 Express.js作为你的框架。如果是,reqres 由路由器自动传递给 registration 函数。这就是 UserService 在此函数中使用它们的方式。 body 属性由 Express 自动创建,如 here 所述.

关于javascript - NodeJs MVC架构,这是通心粉代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com