mongodb 应用_如何使用nestjs mongodb构建可扩展的可维护应用程序应用设计模式

mongodb 应用

There are many kinds of Node Js frameworks that are empowered to enable the developers to build a scalable application based on it. For those developers who tend to gravitate towards OOP (Object Oriented Programming) then Typescript is an approach suitable to build a highly scalable and maintainable application. And NestJS framework has introduced to make things more simple.

可以使用多种Node Js框架来使开发人员能够基于它构建可伸缩的应用程序。 对于那些倾向于使用OOP(面向对象编程)的开发人员而言,Typescript是一种适合构建高度可扩展且可维护的应用程序的方法。 并且引入了NestJS框架,使事情变得更简单。

Nest (NestJS) is an MVC framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with, and fully supports TypeScript.

Nest(NestJS)是一个MVC框架,用于构建高效,可扩展的Node.js服务器端应用程序。 它使用渐进式JavaScript,已构建并完全支持TypeScript

So, in this section, we gonna talk about 3-Tier architecture and how to custom MVC to a 3-Tier architecture in NestJS.

因此,在本节中,我们将讨论3层架构以及如何在NestJS中将MVC定制为3层架构。

A 3-tier architecture is a type of software architecture that is composed of three “tiers” or “layers” of logical computing. They are often used in applications as a specific type of client-server system. 3-tier architectures provide many benefits for production and development environments by modularizing the user interface, business logic, and data storage layers. Doing so gives greater flexibility to development teams by allowing them to update a specific part of an application independently of the other parts. This added flexibility can improve overall time-to-market and decrease development cycle times by giving development teams the ability to replace or upgrade independent tiers without affecting the other parts of the system.

3层体系结构是一种软件体系结构,由逻辑计算的三个“层”或“层”组成。 它们通常在应用程序中用作特定类型的客户端服务器系统。 3层体系结构通过模块化用户界面,业务逻辑和数据存储层,为生产和开发环境提供了许多好处。 这样做允许开发团队独立于其他部分更新应用程序的特定部分,从而为开发团队提供了更大的灵活性。 通过使开发团队能够替换或升级独立的层而不影响系统的其他部分,这种增加的灵活性可以改善整体上市时间并缩短开发周期。

Let’s have a look at figure 1.1 illustrates the architecture of a typical 3-tier monolithic application.

让我们看一下图1.1,它说明了典型的3层单片应用程序的体系结构。

Image for post

Figure 1.1: The architecture of a typical three-tier monolithic application

图1.1:典型的三层单片应用程序的体系结构

Each tier provides services to the tier above: The data tier provides a persistent state, the logic tier executes useful work, and the presentation layer presents the results back to the end-user.

每一层为上一层提供服务:数据层提供持久状态,逻辑层执行有用的工作,表示层将结果提供给最终用户。

To contact into a real-world project. Let’s going to build our application with NestJS and MongoDB, and produce some underlying APIs to help us dive dig into the 3-tier architecture.

接触到一个真实的项目。 让我们使用NestJS和MongoDB构建应用程序,并生成一些底层API,以帮助我们深入研究3层体系结构。

There are some services and packages which are required to set up in your local machine before installing the Nest framework.

在安装Nest框架之前,需要在本地计算机中设置一些服务和软件包。

Those services are NodeJs, MongoDB, Docker, PM2, and some relative tools that are using to manage the services. Please refer to those links below if you are not familiar with those services mention above.

这些服务包括NodeJ,MongoDB,Docker,PM2和一些用于管理服务的相关工具。 如果您不熟悉上面提到的那些服务,请参考下面的链接。

Those services that are mentioned above are paramount importance to take your application running up. We will have an overview of those services and which roles it charge of in the application in the next sections. In this tutorial, I imagine you have installed all services needed in your local machine and ready to start an application.

上面提到的那些服务对于使您的应用程序运行至关重要。 在下一部分中,我们将概述这些服务以及它在应用程序中负责的角色。 在本教程中,我想您已经在本地计算机上安装了所有必需的服务,并准备启动应用程序。

Finally, let’s create our application with Nest via the following commands shows as below:

最后,让我们通过以下所示的命令使用Nest创建我们的应用程序:

  • Install Nest CLI as a global mode: npm i -g @nestjs/cli

    将Nest CLI安装为全局模式: npm i -g @nestjs/cli

  • Create a project with Nest CLI command: nest new "project-name"

    使用Nest CLI命令创建一个项目: nest new "project-name"

There are two packages manager for the JavaScript programming language that is shown in creating project processing. So, you can choose a management tool that you’re frequently using and familiar with you the most. I would recommend using npm.

创建项目处理时显示了两个用于JavaScript编程语言的程序包管理器。 因此,您可以选择一个经常使用并最熟悉的管理工具。 我建议使用npm

Image for post

After creating the project you will see the Nest have initialized some packages and underlying architecture as shown in figure 1.2 below.

创建项目后,您将看到Nest初始化了一些软件包和基础架构,如下图1.2所示。

Image for post

Figure 1.2: Nest underlying architecture from scratch

图1.2:从头开始嵌套底层架构

Let’s take a look at figure 2.1 that we can see into the folder src the folder shows some files which are respective for MVC architecture. Nest framework is highly inspired by Angular with each functionality are bundled in a module. So, the Controller and Service need to be declared in the module before exposure to another module in the application.

让我们看一下图2.1,我们可以看到在src文件夹中,该文件夹显示了一些MVC体系结构的文件。 Nest框架受Angular的启发很大,每个功能都捆绑在一个模块中。 因此,在暴露给应用程序中的另一个模块之前,需要在模块中声明Controller和Service。

Let’s have a look into these other files existing in the project and dig in each file being charge of the role in the application.

让我们看一下项目中存在的其他文件,并挖掘每个负责应用程序角色的文件。

  • Test folder performs the Unit testing functionalities, the TDD performs APIs testing to approach corresponding requirements. Nest integrates Jest, a testing framework to performs the testing in the application.

    测试文件夹执行单元测试功能,而TDD执行API测试以达到相应的要求。 Nest集成了测试框架Jest,可以在应用程序中执行测试。
  • The .eslintrc.js is a configuration file for a static code analysis tool for identifying problematic patterns found in JavaScript code.

    .eslintrc.js是静态代码分析工具的配置文件,用于识别JavaScript代码中发现的有问题的模式。

  • The .prettierrc is a configuration file for opinionated code formatted.

    .prettierrc是用于自定义代码格式的配置文件。

  • The nest-cli.json is a configuration file for command-line interface tool that helps you to initialize, develop, and maintain your Nest applications

    nest-cli.json是命令行界面工具的配置文件,可帮助您初始化,开发和维护Nest应用程序

  • The package.json is a configuration file and on the other hand, we can say this is the heart of your application. All the scripts like an `npm run start` and all dependencies packages like `Jest` are config and declare in this file.

    package.json是一个配置文件,另一方面,我们可以说这是您应用程序的核心。 所有的脚本(如“ npm run start”)和所有的依赖软件包(如“ Jest”)都已配置并在此文件中声明。

  • The package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.

    对于npm修改node_modules树或package.json任何操作,都会自动生成package-lock.json 。 它描述了生成的确切树,因此无论中间依赖项更新如何,后续安装都可以生成相同的树。

  • The tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.

    目录中的tsconfig.json文件指示该目录是TypeScript项目的根目录。 tsconfig.json文件指定了根文件和编译项目所需的编译器选项。

After digging into the based application architecture and configuration files. You finally can start run the application via this command npm run start . The application will start as shown in the figure below.

深入研究基于应用程序的体系结构和配置文件。 您最终可以通过此命令npm run start开始运行应用程序。 应用程序将如下图所示启动。

Image for post

There will be a dist folder created after running the command. This folder contains all our coding in thesrc folder that is compiled all Typescript to Javascript.

运行命令后将创建一个dist文件夹。 此文件夹包含src文件夹中的所有编码,该文件夹将所有Typescript编译为Javascript。

接下来是什么? (What Next?)

Conclusively: In this section, we have dig in the 3-tier architecture and have an overview of underlying Nest architecture. We also create an application with Nest Framework successfully. So, in the next section, we will learn how to connect to MongoDB in our application and custom our application to a 3-tier architecture, which applies some design patterns.

结论:在本节中,我们将深入研究3层架构,并对底层Nest架构进行概述。 我们还成功使用Nest Framework创建了一个应用程序。 因此,在下一部分中,我们将学习如何在我们的应用程序中连接到MongoDB,以及如何将我们的应用程序定制为3层架构,该架构采用了一些设计模式。

Let’s have a look at the next section. Thanks for reading!

让我们看下一节。 谢谢阅读!

The next section (part2): https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-50112e9c99b4

下一部分(第2部分)https : //medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-50112e9c99b4

Github source: https://github.com/phatvo21/nest-demo

Github来源https : //github.com/phatvo21/nest-demo

List of content:

内容清单:

Part 1: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-2f71c060652

第1部分https : //medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-2f71c060652

Part 2: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-50112e9c99b4

第2部分https//medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-50112e9c99b4

Part 3: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-7b287af61354

第3部分: https //medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-7b287af61354

Final Part: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-789df9782959

最后一部分: https //medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-789df9782959

翻译自: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-2f71c060652

mongodb 应用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值