Written in the beginning

I’m so sorry! Today (December 13, 2018 10:01:41) to see any more reading quantity, I write this is himself a share for the company, the node from the shallow to the deep column to some relevant knowledge, and then have a whole questions I will probably arrange a issued out, didn’t expect to have more than 1000 reading quantity, embarrassed, smoke a time I tidy it up, Let’s do something simple, logical, sorry, sorry

What problems can Node solve?

The primary goal of Node is to provide a simple, easy-to-use development tool for creating high-performance servers. The bottleneck of Web servers is concurrent users, compared to Java and Php implementations

Node has significant performance advantages in high concurrency,I/O intensive scenarios

  • High concurrency refers to concurrent access to the server at the same time
  • I/O intensive refers to the file operation, network operation, database, the relative CPU intensive,CPU intensive refers to the logical processing operation, compression, decompression, encryption, decryption

In fact, the biggest reason for the popularity of Node is the popularity of the front end, after the attention of the front end, in the tide of the big front end to the peak, because the main scene of the Web is to receive the client request to read static resources and render the interface, so Node is very suitable for the development of Web applications

What is Node?

Node.js is a JavaScript runtime based on the Chrome V8 engine. Node is not a language It allows JS to run at the back end of the runtime and does not include the full set of javascript, since DOM and BOM are not included in the server. Node also provides some new modules such as HTTP and FS modules. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, and Node.js’s package manager, NPM, is the largest open source library ecosystem in the world.

My personal understanding of node event polling is that the main thread allocates tasks and registers callback functions. IO when executing code in the main thread to wait, the last in the main thread has been completed road wheel, because this time the main thread is idle, so can always polling, until he found a certain IO operation to send the signal to tell him I’m done now, you can use this data, the main thread of the resources to implement the callback function. That is, the main thread is doing the work all the time, either executing the function or polling for events to see which event has completed its callback.

Maybe it’s called single-threaded asynchrony

Of course, many kids may ask, why is JS single threaded? Javascript was originally designed to be single threaded. Wouldn’t it be confusing if multiple threads were manipulating the DOM at the same time? By single-threaded I mean that the main thread is single-threaded, so in Node the main thread is still single-threaded.

A word about processes and threads, by the way

Let me draw a picture of the browser

  • User interface – includes address bar, forward/back buttons, bookmark menu, etc
  • Browser engine – passes instructions between the user interface and the rendering engine (the main process of the browser)
  • Rendering engine, also known as browser kernel (browser rendering process)

I checked some information, in fact, rendering engine internal is multi-threaded, internal contains two of the most important threads UI thread and JS thread. It is important to note that the UI thread and the JS thread are mutually exclusive, because the results of the JS run affect the results of the UI thread. UI updates are stored in the queue and executed as soon as the JS thread is idle.

Other threads involve Event loops, which you can Google for yourself.

About the buffer

So why do we need to use a buffer to represent node? Buffer A Buffer is a portion of memory that temporarily stores input and output data. The JS language has no binary data types, and you must deal with binary data when dealing with TCP and file streams. It seems that es6 also has a TypedArray, should be similar.

Node official website buffer tickets

Node readable and writable streams

Write before, now basically forget ~~ flow related – air tickets

let http = require('http'); //req stream object is a readable stream //res is a writable stream writelet server = http.createServer();
let url = require('url'); // Execute callback server.on() when the client connects to the server'connection'.function () {
    console.log('Client connection'); }); // Req represents the connection to the client. The server parses the request information from the client and places it on the req.'request'.function(req, res) { console.log(req.method); // Get the request method namelet { pathname, query } = url.parse(req.url, true); console.log(pathname); console.log(query); console.log(req.url); // Get the request path console.log(req.headers); // Request header objectlet result = [];
    req.on('data'.function (data) {
        result.push(data);
    });
    req.on('end'.function () {
        letr = Buffer.concat(result); // Request body console.log(r.tostring ()); Res.end (r); })}); server.on('close'.function (req, res) {
    console.log('Server down');
});
server.on('error'.function (err) {
    console.log('Server error');
});
server.listen(8089, function () {
    console.log('server started at http://localhost:8089');
});
Copy the code

Did not write, the subsequent collation ~