NodeJS需要一个文件中的所有模块,这是一个好习惯吗? [英] NodeJS require all modules in one file, good practice?

查看:182
本文介绍了NodeJS需要一个文件中的所有模块,这是一个好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何缺点,从设计或安全的角度来看,有一个文件需要()我需要的所有模块,然后出口他们。这样可以节省我跟踪每个文件中的所有模块。

I am wondering if there is any downside, from a design or security point of view, having one file that requires() all the modules that I need, and then exports them. This would save me to keep track of all modules in every single file.

示例:

// my_requires.js
const bodyParser = require('body-parser')
const parseForm = bodyParser.urlencoded({extended: false})

const DOMPurify = require('dompurify');
const {JSDOM} = require('jsdom');

const jwt = require('jsonwebtoken');
const passport = require('passport');
require('../config/passport')(passport)

module.exports = {
    bodyParser: bodyParser,
    parseForm: parseForm,
    jwt: jwt,
    passport: passport,
    bcrypt: bcrypt
}

然后在我需要它们的任何文件中,我只需要

and then in any file where I need them, I would just need to

const reqs = require('my_requires.js')

我无法弄清楚使用像这样的构造是否有任何缺点。由于模块都被加载到全局空间中,我没有看到我的方法的任何缺点?

I couldn't figure out if there is any downside in using a construct like this. Since the modules are all loaded into the global space I don't see any downside of my approach?

推荐答案

为了良好的模块化设计这使得模块最容易在其他项目中重用,模块应该只包含它自己需要的东西,或者它必须与其他人共享。

For good modular design that makes modules easiest to reuse in other projects, a module should include ONLY the things that it needs itself or that it absolutely has to share with others.

对于某些其他模块而言,它不应该只需要在其他地方保存几个字母。没有理由这样做。模块由系统缓存,因此在需要它的模块中使用 require()而不是从其他文件导出导致模块更独立,更多独立并减少不必要的依赖。

It shouldn't require in things for some other module just to save a few letters of typing somewhere else. There's just no reason to do that. Modules are cached by the system so using a require() in a module where it is needed rather than getting an export from some other file makes the module more stand-alone, more independent and reduces unnecessary dependencies.

所以,如果你 require()一切都在一个地方,然后导出它,你只需创建一个巨大的相互依赖的项目,其中没有任何东西独立于其他任何东西。在其他项目中,单个模块本身不能重复使用,因为一切都取决于特定于应用程序的核心应用程序文件。而且,你通过摆脱模块独立性实际获得了什么?可能只是保存了几行键入 - 这就是全部。

So, if you require() everything in one place and then export that around, you just create a gigantic interdependent project where nothing stands independent of anything else. No single module can be reused by itself in some other project because everything depends upon the core app file which is app-specific. And, what did you actually gain by getting rid of module independence? Probably just saved a few lines of typing - that's all.

当从未实现模块化的其他编程环境加速node.js时,这有点违反直觉node.js的方式,但每个模块应该以它所依赖的外部库的 require()语句列表开头。是的,其中一些代码将在其他模块中重复,但我不认为它是不必要的重复,而是为了清晰和模块化而有组织的依赖性声明。这是该模块所依赖的内容。以下是为了重用此模块必须安装的内容。以下是如何使用此模块独立于应用程序的其余部分。

It is somewhat counterintuitive when coming up to speed in node.js from other programming environments that don't implement modularity the way node.js does, but each module should start with a list of require() statements for the external libraries that it depends on. Yes, some of that code will be duplicated in other modules, but I don't think of it as unnecessary duplication, but rather as an organized statement of dependencies in the interest of clarity and modularity. Here's what this module depends on. Here's what must be installed in order to reuse this module. Here's how to use this module independent of the rest of the app.


我无法弄清楚使用a是否有任何缺点像这样建造。由于这些模块都被加载到全局空间中,我认为我的方法没有任何缺点?

I couldn't figure out if there is any downside in using a construct like this. Since the modules are all loaded into the global space I don't see any downside of my approach?

没有什么是黑白的。有时候有理由分享一组东西。但是,我将这些缺点列入您的方法,我通常会避免使用该技术:

Nothing is black and white. Sometimes there are reasons to share a group of things. But, I'd list these downsides to your method and I would generally avoid the technique:


  1. 降低模块独立性和模块可重用性。

  2. 查看代码时模块依赖性不那么明显。

  3. 使模块难以独立测试。

  4. 创建不需要相互依赖的文件之间不必要的相互依赖性,以便加载他们想要使用的东西。

  1. Reduces module independence and module reusability.
  2. Makes module dependencies less obvious when looking at the code.
  3. Makes modules harder to test independently.
  4. Creates unnecessary interdependencies between files that don't need to depend upon each other for loading things they want to use.






有时候你会发现很多模块都需要同时使用一组通用的模块。在这种情况下,不要将它们从应用程序导出到所有内容中。相反,创建一个新的可共享模块,导入其他模块并导出它们。然后,您将核心模块保持独立,并创建了一个新的可重用模块,该模块可以为您提供一组通常一起使用的模块。通过这种方式,您可以通过增加模块性和可重用性而不是通过减少它来解决同样的问题(并且您也可以节省一些打字)。


There are occasionally cases where you find that there are a common set of modules that a lot of your modules need all at once. In that case, don't export them from the app into everything. Instead, make a new shareable module that imports those other modules and exports them. Then, you've kept your core modules independent and made a new reusable module that gets you a group of modules you commonly use together. In this way, you solve the same problem by increasing modularity and reusability rather than by reducing it (and you get to save some typing too).

这篇关于NodeJS需要一个文件中的所有模块,这是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆