简体   繁体   English

NodeJS应用架构最佳实践

[英]Best Practice of NodeJS Application Structure

We have Nodejs projects, One of them is the main project.我们有 Nodejs 项目,其中一个是主项目。 We are using this (Main Project) functions in other projects.我们在其他项目中使用这个(主项目)功能。 We install our main project as a package with NPM to other projects, then uses the functions that we need.我们将我们的主项目安装为 package 和 NPM 到其他项目,然后使用我们需要的功能。

But this way making many and tied dependency between main project and other projects.但是这种方式在主项目和其他项目之间产生了许多紧密的依赖关系。 Every time that we change main project, we need to update it's version in other projects with NPM install in other projects.每次我们更改主项目时,我们都需要在其他项目中更新它的版本,并在其他项目中安装 NPM。

Please give me your idea for the best practices in this case.请告诉我您在这种情况下的最佳做法的想法。 How we can handle this dependencies between the main project and other projects?我们如何处理主项目和其他项目之间的这种依赖关系?

Thanks谢谢

Let's start with the global pattern and wording.让我们从全局模式和措辞开始。 If a box of functions is used as a toolbox for other projects, you shouldn't think of it as the main project.如果一盒函数被用作其他项目的工具箱,你不应该把它当做项目。 It is just a library to be required.它只是一个需要的库。 Call it a basic library if you like.如果您愿意,可以将其称为基本库

A main project would be the one and only top-level package, that requires all other packages.一个主要项目将是唯一的顶级 package,它需要所有其他包。 As you have multiple projects, the term main project is just not in place.由于您有多个项目,因此主要项目一词不合适。

A framework is a more complex case.框架是一个更复杂的案例。 It seems that's not, what you are talking about.好像不是,你在说什么。 If it is a common framework, I still would call it framework and not the main project.如果它是一个通用框架,我仍然会称它为框架而不是项目。

Now having set the parts into the appropriate places, let's talk about dependencies.现在已经将部分设置到适当的位置,让我们谈谈依赖关系。 All projects depend on this one toolbox.所有项目都依赖于这一工具箱。 If this toolbox is well coded, it will work with all projects.如果此工具箱编码良好,它将适用于所有项目。

If it does not work with all projects, there is code in it, that is not general, but specialised for a single project.如果它不适用于所有项目,则其中有代码,这不是通用的,而是专门针对单个项目的。 This part of the code is the culprit.这部分代码是罪魁祸首。 The code that is special for a single project, should be injected as a callback function.单个项目专用的代码,应作为回调 function 注入。 Then the toolbox will execute it, but the special code is still in the codebase of the special project, because the callback is defined there.然后工具箱会执行它,但是特殊代码仍然在特殊项目的代码库中,因为那里定义了回调。 This kind of callback injection is one of the most powerful parts of javascript.这种回调注入是javascript最强大的部分之一。

Then write tests for your code.然后为您的代码编写测试。 Writing tests is the best advisor of clean injection and good architecture.编写测试是干净注入和良好架构的最佳顾问。 I also hint to the term "dependency injection".我还暗示了“依赖注入”这个术语。

All this sounds great, but if you adjust the common library, as a side effect other projects still break, right?所有这些听起来都很棒,但是如果您调整公共库,作为副作用,其他项目仍然会中断,对吗?

This is not a question of architecture but of maintenance and the release cycle of the library.这不是架构问题,而是库的维护和发布周期问题。 The API is extended in minor releases. API 在次要版本中进行了扩展。 Breaking changes should only happen in major releases.重大更改应该只发生在主要版本中。 If you upgrade to a major release, all projects need to be adjusted to the changed API.如果升级到主版本,所有项目都需要调整为更改后的 API。 Hence, breaking releases should be rare.因此,中断版本应该很少见。

Again, full test coverage helps to quickly adjust the code to a major release.同样,完整的测试覆盖有助于快速将代码调整为主要版本。 The tests reveal every breaking function with one call the tests.测试揭示了每一个破坏 function 的测试。

You still need a migration path from your current situation to a satisfying situation.您仍然需要从当前情况到令人满意的情况的迁移路径。 Here the answer is the git repository.这里的答案是 git 存储库。 Create a branch of the basic library for every project, to keep your business up and running.为每个项目创建一个基本库的分支,以保持您的业务正常运行。 Then merge the different branches into one abstracted stable branch over time.然后随着时间的推移将不同的分支合并为一个抽象的稳定分支。

So my answer has five parts:所以我的回答有五个部分:

  1. Replace the term "main project" with "base library" in your mindset.在您的思维方式中将术语“主项目”替换为“基础库”。

  2. Work with different branches in git as a migration path.使用 git 中的不同分支作为迁移路径。

  3. Then learn about injection, injection if form of callbacks and injection of dependencies.然后了解注入,回调形式的注入和依赖项的注入。

  4. Test your code as far as possible.尽可能测试你的代码。 There are even more reasons to do it.还有更多的理由这样做。

  5. Finally follow a standard release cycle for the base library.最后遵循基本库的标准发布周期。

And as you are asking for best practice, the answer to point five is "Semantic Versioning".当您要求最佳实践时,第五点的答案是“语义版本控制”。 https://semver.org https://semver.org

Following semver your package will cleanly work the the version based dependency manager of npm .semver之后,您的 package 将干净地工作npm的基于版本的依赖项管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM