【Nodejs】Introduction to Node.js

insert image description here

1 Introduction


The importance of Node is self-evident. Many Internet companies already have a large number of high-performance systems running on Node. Node achieves extremely high performance benchmarks with its single-threaded, asynchronous, and more. In addition, the most popular web development model currently is the form of front-end and back-end separation, that is, front-end developers and back-end developers develop independently on their favorite IDE, and then realize the interaction between data and processes through HTTP or RPC. Guided by the powerful functions of Node, this development mode has become more and more efficient, and it is also more and more favored by various Internet companies.

1.1 Why do front-end students learn back-end/Why do back-end students learn front-end

  • Understand the front-end and back-end interaction process.
  • Front-end students can better integrate and communicate more smoothly with programmers who develop in the background.
  • When the business logic of the website needs to be front-end, the front-end personnel need to learn some background development technologies to complete the corresponding tasks; and vice versa.
  • Broaden the knowledge horizon and technology stack, and be able to examine the entire project from a global perspective.

1.2 Why should front-end students learn Node.js

(1) Node.js uses JavaScript language to develop server-side applications, which is convenient for front-end students to get started (some companies even require front-end engineers to master Node.js development).

(2) It realizes the unification of front-end and back-end grammars, which is conducive to the integration with front-end codes, and even shares some codes.

For example, for the various fields returned by the interface, both the front and back ends must be verified. At this point, if Node.js is used for background development, the front and back ends can share the verification code.

(3) Node.js has high performance and an active ecosystem, providing a large number of open source libraries.

(4) Jeff Atwood proposed the famous Atwood's Law in 2007: Any application system that can be implemented with JavaScript will eventually be implemented with JavaScript. It doesn't matter who Jeff Atwood is (he's the co-founder of the Stack Overflow website), what matters is this law.

2. What is Node.js?


2.1 Official Definition

Node.js is a JavaScript runtime environment based on the Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. Node.js' package management tool npm is the world's largest open source library ecosystem. Node.js is not a language, nor is it a framework for JavaScript, nor is it a web server like Nginx. Node.js is an operating environment (platform) for JavaScript on the server side.

2.2 The composition of Node.js

How does running JavaScript in Node.js differ from running JavaScript in Chrome?

Both use the same JS engine. Writing JS in Node.js is almost the same as writing JS on the front end. The difference in writing is: Node.js does not have APIs related to browsers and page tags, but some APIs related to Node.js have been added. In layman's terms, for developers, writing JS on the front end is used to control the browser; while writing JS in the Node.js environment can control the entire computer.

We know that the composition of JavaScript is divided into three parts:
ECMAScript

  • DOM: APIs related to label elements
  • BOM: browser-related API
  • ECMAScript is the syntax of JS; DOM and BOM browser-side API for JS.

The composition of Node.js is divided into:

  • ECMAScript. All syntax of ECMAScript can be used in the Node environment.
  • Some additional APIs provided by the Node environment (including file, network and other related APIs).

As shown below:
insert image description here

2.3 Summary

  • Node is a server-side JavaScript interpreter
  • Node.js is a JavaScript runtime environment based on the Chrome V8 engine
  • Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient
  • Node.js' package manager npm is the world's largest open source library ecosystem
  • Node.js is a dynamic language, Javascript running on the server

3. Application of Node.js


Node.js has a strong developer community, and now it has developed a relatively mature technical system and a huge ecosystem. It is widely used in Web services, development workflow, client applications, and many other fields. Among them, in the field of Web services, the industry has the highest acceptance of Node.js.

3.1 BFF middle layer

BFF, that is, Backend For Frontend (serving the backend of the frontend). Yubo pointed out the concept of BFF layer in the article "Evolution from front-end technology to experience technology" :
BFF 模式下,整体分工很清晰,后端通过 Java/C++ 等语言负责服务实现,理想情况下给前端提供的是基于领域模型的 RPC 接口,前端则在 BFF 层直接调用服务端 RPC 接口拿到数据,按需加工消费数据,并实现人机交互。基于 BFF 模式的研发,很适合拥有前端技术背景的全栈型工程师。这种模式的好处很明显,后端可以专注于业务领域,更多从领域模型的视角去思考问题,页面视角的数据则交给前端型全栈工程师去搞定。领域模型与页面数据是两种思维模式,通过 BFF 可以很好地解耦开,让彼此更专业高效。

In the Web service, build a middle layer, the front end accesses the interface of the middle layer, and the middle layer accesses the Java/C++ service in the background. The characteristic of this type of service is that it does not require too much server computing power, but it has high requirements for the flexibility of the program. These two features coincide with the advantages of Node.js. Node.js is very suitable for BFF layer, the advantages are as follows:

  • For the front-end: Let the front-end have the ability to freely assemble background data, which can reduce a lot of business communication costs and speed up business iteration; moreover, front-end students can independently decide how to communicate with the background.
  • For the background and operation and maintenance, the benefits are: security (the main server will not be exposed to the outside), reducing the complexity of the main server, etc.

3.2 Server-side rendering

Client side rendering (CSR / Client side render): The front end requests data through a large number of interfaces, and then dynamically processes and generates page structure and display through JS. The advantages are separation of front and back ends, reduced server pressure, and partial refresh. The disadvantage is that it is not conducive to SEO (if your page then obtains content asynchronously through Ajax, the crawler will not wait for the asynchronous completion before crawling the page content), and the rendering of the first screen is slow.

Server Side Rendering (SSR / Server Side Render): What the server returns is not the interface data, but the HTML string of the entire page (or the entire floor), which can be directly displayed by the browser. In other words, it is rendered directly on the server side, and then packaged and returned to the front end at one time. The advantage is that it is good for SEO and the first screen rendering is very fast.

Summary: Search engine optimization + first screen speed optimization = server-side rendering.

Remarks: The "server-side rendering" here is just to use Node.js as the middle layer, it will not replace the back-end, please rest assured.

Reference link:
The concept of Vue server-side rendering
The difference between server-side rendering (SSR) and client-side rendering (CSR), and some thoughts on optimizing the first screen
Server-side rendering (SSR)
should hang

Historical review:
(1) At the beginning, the page was very simple, and the html was rendered by the backend (such as PHP, ASP, JSP, etc.). The backend found that the js in the page is very troublesome (although it is simple, but there are many pitfalls), so the company asked the company to recruit someone who specializes in writing js, referred to as "front-end cut picture boy".

(2) With the rise of Node.js and front-end MVC, and the increasingly complex front-end, it gradually evolved into "separation of front-end and back-end".

(3) After the front-end SPA application became popular, it was found that SEO was a big problem, and the rendering speed of the first screen was very slow, but no matter how difficult the path I chose, I had to go on, so using Node.js to render on the server was regarded as a a way out.

(4) When we were together in the past, the back-end did part of the front-end work; when we are together now, the front-end does part of the back-end work.

3.3 Backend for small services and small websites (based on Express and Koa frameworks)

Many companies now use Node.js to develop interfaces for their background management systems. After all, background management systems do not have high requirements for performance and concurrency. With Node.js, you can directly operate the DB through JS, add, delete, modify and check, and generate interfaces, which greatly reduces the learning threshold for front-end students.

Of course, sometimes doing Node.js development is because there is not enough manpower in the background, so part of the workload of background development is transferred to the front-end students.

3.4 Make a project construction tool

The construction tools gulp and Webpack that are widely used in the front end are implemented based on Node.js.

3.5 Make PC client software (based on Electron framework)

The Electron framework is based on Node.js and can be used to develop client software.

Electron, formerly known as Atom Shell, is an open source framework developed by GitHub. Electron uses Node.js as the runtime and chromium as the rendering engine, enabling developers to use JS, a front-end technology stack, to develop cross-platform desktop GUI applications.

You may be surprised by one thing: the code editor VS Code software that programmers are using is developed based on the Electron framework. Other well-known applications developed using Electron include: Skype, GitHub Desktop, Slack, WhatsApp, etc.

Another example is: Twitch, a video game live broadcast website, is known as the originator of foreign game live broadcasts. Its client software on the PC side uses the Electron framework. You will find that Twitch's website vision is almost the same as the PC-side vision. If both ends use the JS language, existing projects can be greatly reused.

3.6 Well-known Node.js open source projects

insert image description here

  • express: A well-known web service framework in Node.js.
  • Koa: A next-generation Web services framework for Node.js. The so-called "next generation" is relative to Express.
  • Egg: In 2016, Alibaba developed the well-known Egg.js open source project, known as an enterprise-level Web service framework. Egg.js is developed based on Koa.
  • mocha: It is the most popular JavaScript testing framework, which can be used in both browser and Node environment.
  • PM2: node multi-process management.
  • jade: very good template engine, not limited to js language.
  • CoffeeScript: Showcasing the best parts of JavaScript in a concise way.
  • Atom: editor.
  • VS Code: The coolest editor.
  • socket.io: A real-time communication framework.

3.7 Summary

Perhaps, the background application that can be done with Node.js can also be done with Java/C++; but Node.js can give us another choice.

In the short term, it is difficult for Node.js to become the main development language in the background like Java/C++. This is not because of the performance problem of Node.js, but mainly because Node.js is still relatively young, with too little experience and insufficient framework support. For enterprise-level services, Node.js is no match for Java/C++, so it can only do "lightweight" at present; but the future can be expected.

What limits language ability is not language itself, but ecology.

4. Features of Node.js

  • Asynchronous, non-blocking IO model
  • event loop
  • single thread
  • Summary: Lightweight and Efficient

Node.js is very performant and efficient.

In the traditional Java language, a request starts a thread, and when the request is processed, the thread is closed. Node.js does not use this model at all, it is essentially a single thread.

You may wonder: How does a thread serve a large number of requests and how to deal with high concurrency? This is because Node.js uses an asynchronous, non-blocking model.

The so-called "single thread" here means that there is only one main thread of Node. In order to ensure that the main thread is not blocked, the main thread is used to receive client requests. But will not deal with specific tasks. There is also a thread pool behind Node, which handles long-running tasks (such as IO operations and network operations). The tasks in the thread pool are executed through the mechanism of queue and event loop.

5. Disadvantages when using Node.js


  • The program is running unstable, and the service may be unavailable
  • The running efficiency of the program is low, and the number of requests per second is maintained at a low level
  • Front-end students are not familiar with server-side technology.

Guess you like

Origin blog.csdn.net/weixin_43094619/article/details/131896109