preface

After more than a month, I came back. This article is also a series of articles, the content is teacher Piao Ling wrote “simple NodeJS” reading notes. Node takes Javascript out of the browser and becomes more than just a tool for designing dynamic web pages and user interaction on the browser side. This article is the first in a series of reading notes on NodeJS. Therefore, first of all, I need to introduce how teacher Piao Ling arranges the content of this book. Next, a brief introduction to nodeJs. Finally, a few concepts that are easy to overlook.

The architecture

The book has eleven chapters, which I think can be divided into seven parts:

  • Part I -> Chapter 1: A brief introduction to the history and background of nodeJS
  • Part 2 -> Chapter 2: Introduces the module mechanism of nodeJS, and learns about nodeJS in terms of code organization
  • Part 3 -> Chapters 3 and 4: Introduce the operation principle of asynchronous IO and asynchronous programming, from the running structure to understand nodeJS
  • Part 4 -> Chapters 5 and 6: nodeJS memory management
  • Chapter 5 -> Chapter 7, Chapter 8: Network programming related content, namely how to use nodeJS to implement TCP, UDP, HTTP and other network protocols
  • Part 6 -> Chapter 9: Introduces node multi-process technology and how to use multi-process to improve application performance
  • Part 7 -> Chapters 10 and 11: Covers testing and engineering

For a beginner like me who doesn’t have much exposure to nodeJS, I find it a bit difficult to organize the code just by looking at how nodeJS works. So I decided to start with the application, whatever it was, to be able to use this thing first, and then worry about how it works. So, the order of reading notes here becomes: Part 1, Part 5, Part 6, Part 7, Part 2, Part 3, Part 4.

Introduction of the node

As for the introduction to Node, the first thing I would normally do is to define nodeJS. However, I have been thinking about this definition for a long time and feel unable to find the right one. So, to borrow a blog definition, Node.js is simply JavaScript that can run on the server side. This raises the question, why does it run on the server? Next, follow the footsteps of Teacher Pu Ling to reveal the mystery of Node a little bit.

The birth of the node

To understand something, we must first know how it came to be. About node is how to come to this thing, I will do a porter, move the content of Pu Ling teacher:

  • In March 2009, Ryan Dahl announced on his blog that he was going to create a lightweight Web server based on V8 and provide a set of libraries
  • Ryan Dahl released the original version on Github in May 2009
  • In December 2009 and April 2010, two JSConf conferences scheduled lectures on Node
  • At the end of 2010, Node was funded by Joyent, a Silicon Valley cloud computing service, and its founder Ryan Dahl joined Joyent full-time to lead the development of Node
  • Node was released for Windows in July 2011 with support from Microsoft
  • In November 2011, Node passed Ruby on Rails as the most watched project on Github (later surpassed by Bootstrap).
  • At the end of January 2012, Ryan Dahl, satisfied with the Design of The Node architecture, handed over the REINS to Isaac Z. Schlueter turned to some research projects on his own. Isaac Z. Schlueter is the author of Node’s package manager, NPM, and takes over Node’s release and bug fixes.

The node with JavaScript

The relationship between Node and JS can be explained in one sentence: Node is JS running on the server, the essence of JS, but the engine is no longer a browser. So why write the server in JS? The problem starts with the author. RyanDahl, the author of node, is a veteran c/c++ programmer who worked around high-performance web servers before creating node. Through his research, he found several key points for designing high-performance Web servers:Event-driven, non-blocking IO.

Compared to other languages: C is a high barrier to development, Haskell authors can’t handle it themselves, Lua already has a lot of IO blocking libraries, and Ruby virtual machines don’t perform well. At this point, JavaScript has a low barrier to entry, no historical baggage (because it’s relatively new), and most importantly, it fits both high performance (supported by the V8 engine) and event driven. As a result, JavaScript is the new darling.

Previously we introduced JS to Node. Now we will introduce Node to JS.The obvious conclusion is that Node takes JS out of the browser and makes it run on the server. First, let’s compare the differences between Chrome and Node:As you can see, the difference between Node and Chrome is a WebKit engine. The WebKit engine is a layout engine. In other words, Node and Chrome are structurally very similar when it comes to processing parts of JS code. JS originally needs browser support, but the emergence of Node will JS from the browser out, so that JS can also complete processing database, build websocket server, and so on some directly with the operating system to deal with the server.

The characteristics of the node

Node is a back-end JavaScript platform that retains some of the interfaces in the front-end browser JavaScript and presents features that are different from those of other languages.

  1. Asynchronous I/o
  2. event-driven
  3. Single thread

Some of the concepts

Page rendering process

The front-end page rendering process is shown in the figure below. It is mainly divided into three stages:

  1. URL – > the DOM tree
  2. DOM -> Webkit drawing context
  3. Drawing Context -> Final rendering

JS engine

The JS engine processes JS code as follows:

Some basic concepts about the network

  • Socket: Simply put, a socket can be understood as a data channel connected between two computers sending and receiving data. Data flows along this channel and finally reaches its destination. The operation of sending and receiving data is divided into four stages:
    1. Creating the socket (Creating the socket phase)
    2. Connect the pipe to the socket on the server side (connection phase)
    3. Sending and receiving data (communication phase)
    4. Disconnect the pipe and remove the socket (disconnect phase)
  • TCP/UDP: two protocols at the transport layer. TCP is connection-oriented and UDP is non-connection-oriented. Thus, TCP is more stable and UDP is more efficient.
  • HTTP: application layer protocol. HTTP is a further encapsulation of TCP.
  • Ajax: Ajax is not a JavaScript specification, but is simply an acronym: Asynchronous JavaScript and XML, which means that JavaScript performs Asynchronous network requests.

The words in the back

This is the first article in the NodeJS book Notes series, which feels a bit like a popular science article.