


Let's talk about how the node framework Nest.js integrates Express with loose coupling!
This article will let you talk about how the node framework Nest.js integrates Express in a loosely coupled manner. I hope it will be helpful to everyone!
Nodejs provides the http module for listening to ports, processing http requests, and returning responses, which is what it mainly does.
But the api of the http module is too primitive, and it is troublesome to process request responses directly based on it, so we will encapsulate it with a library such as express.
What this layer does is to add a lot of request and response processing methods to request and response to meet the needs of various scenarios, and handle routing, and also provide middleware call chains. To facilitate the reuse of some code, this middleware call chain is called the onion model.
#But this layer does not solve the architectural problem: what to do when there are too many modules, and how to manage them? How to divide Model, View and Controller? etc.
So, when using Node.js for back-end services, we will include another layer to solve the architectural problem. The frameworks at this layer include eggjs (ant), midwayjs (Taobao), nestjs (foreign) ).
nestjs is one of the best:
The adapter mode is when a function implemented by a third party is used, it does not rely directly on it, but defines a layer of interfaces to allow the third party to adapt to this layer of interfaces. In this way, any solution that adapts to this layer of interfaces can be integrated, and solutions can be switched flexibly.
Nest.js provides a layer of interface (HttpServer) for the underlying http platform and defines a bunch of methods:import { Controller, Get, Request } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() findAll(@@Request() request: Request): string { return 'This action returns all cats'; } }If you want to call some platform-specific methods outside of the interface, Nest.js also supports it, then use @Req to inject:
import { Controller, Get, Req } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() findAll(@@Req() request: Request): string { return 'This action returns all cats'; } }What is injected in this way is a specific platform For example, express's native request object can directly use all its methods. In addition, if you really want to use the specific api of the Express platform, you can specify the corresponding type parameters when NestFactory.create, so that you can do the corresponding type prompts and checks:
But this will be coupled with a specific platform. Unless you are sure that you will not switch platforms, it is not recommended.
The http platform does this, and similarly, the websocket platform does the same:
defines a unified interface, which can be flexibly connected to socketio and websocket through adapters. Switch:
Illustration of Nest.js processing of http and websocket platforms:
Summary
Node.js provides the http module to monitor ports and process request responses, but its API is too primitive, so we will include a layer to provide more useful request and response APIs in the express layer, but This layer does not solve the architectural problem. To introduce MVC, IOC and other architectures, another layer needs to be included, using higher-level back-end frameworks such as Egg.js, Midway.js, and Nest.js, among which Nest.js is the best. .
Nest.js has made special designs to integrate with the underlying http platform. It uses the adapter mode to provide a layer of interfaces for the underlying platform to adapt, so that different http platforms can be flexibly switched. .
But it also supports the use of platform-specific APIs. For example, @Req can be used to inject the underlying request object in the controller, and the type parameters of the corresponding platform can also be passed in when creating a container.
Nest.js uses Express by default, but it is not entirely correct to say that Express is used, because you can flexibly switch to others. This is the beauty of the adapter pattern.
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of Let's talk about how the node framework Nest.js integrates Express with loose coupling!. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
