Evolution from simple http server to nestjs
Let’s talk about the http
module first. The http
module is a library natively provided by Node to process HTTP requests and responses. It is also the most important module when developing a webserver.
The usage of the http
library is generally like this:
Expressed by a diagram, that’s it:
As a module natively provided by Node, its role is to parse HTTP requests and generate HTTP responses, so the API it provides is very flexible. We can get the requested URL through req.url
, get the method of request message through req.method
, and send the response message through res.end
.
But if we need to complete some slightly complex requirements, it is not very convenient to use the http
module.
For example, our project has such requirements:
- When the user visits
/artciles/2
, then the content of the article with id 2 is returned; - When the user visits
/users/325
, then the information of the user with id 2 is returned.
To use the http
module directly, you may write like this:
Obviously, this way of writing is bloated and inefficient. This is because the http
module can only be regarded as a library, not a framework. At this time we need web frameworks such as Express and Koa.
Express
Express, Koa, Fastify, etc. belong to the Web framework. They are based on Node’s http
module and encapsulate some commonly used functions when developing web servers, such as routing.
The usage of the Express is generally like this:
In diagram:
With the routing function provided by Express, we no longer need to write all kinds of inefficient if
statements as before. When processing the HTTP response, you can also directly return the JSON file through res.json()
, and use res.sendFile()
to return the binary file. The Express framework will automatically set the appropriate content type for us, instead of manually setting the ContentType.
But lightweight frameworks such as Express still haven’t solved some problems:
- (In a large-scale project,) how to design the architecture of the project?
- How to design the directory structure of the project?
At this time, heavyweight frameworks such as Nestjs and Eggjs are needed.
Nestjs
Frameworks such as Nest require us to use a fixed file directory structure and use a specific project architecture. They don’t directly handle HTTP requests and responses themselves, and the specific work is handed over to low-level frameworks such as Express or Fastify.
The file directory structure generated by Nest is as follows:
The architecture of Nest Project is generally like this:
Conclusion
The feature provided by the http
module is very primitive and suitable for developing frameworks, but not suitable for direct use in the development of normal web projects.
Frameworks such as Express provide appropriate packages based on HTTP modules, which are suitable for developing small and medium-sized projects.
Frameworks such as Next are based on Express, Koa, etc. to do secondary packaging, suitable for the development of more complex projects.
Finally, eat a hamburger.
Comments