Earlier, someone from Knowledge Planet asked the question: What is the difference between a Node environment and a browser environment? This kind of thinking is worth encouraging. Learning by contrast makes it easy to visualize an abstract knowledge. The answer at that time was just a simple drawing of a picture, I feel it is necessary to use this article to dig deeper

What is the front end and what is the back end?

Presumably no students do not know that Node will Js extension to the back end, although the front end and back end in the ape world is a high-frequency vocabulary, I am afraid that may not be able to answer clearly what is the front end, what is the back end of this problem?

The answer lies in every student’s subconscious: The Web front end refers to the things that users can see and touch, including the UI level and the interactive implementation of the Web level; The back-end is more about interacting with the database to handle the corresponding functions, data access, platform stability, performance, and so on

I want to make a distinction at the communication level. Web applications are basically built on HTTP/HTTPS, with the front end (or as we extend to the big front end: Android, IOS, browser, etc.) as the HTTP/HTTPS Client and the back end as the protocol server

Combining the above two points, a simple graph can be drawn:

A picture that is likely to be teased

To serve our understanding of Node, Js running in a browser primarily operates on the Dom (UI level) and acts as an HTTP client for network communication. Node, on the server side, is no longer limited by the browser sandbox, can start HTTP services (and websockets), operate databases, easily read and write disk files, and much more

Third, Node and browser components are compared

A simple diagram copied from Node

It’s time to come up with a diagram to answer the planet of Knowledge question. Before Node came along, Js was primarily run in a browser (WebView on Android/IOS is also a browser). Browsers are made up of a layout engine (also known as the browser kernel) and a Javascript engine. In Chrome’s case, the layout engine is WebKit (the older version has been replaced with Blink). The main function of a layout engine is to parse Html Dom and Css style rules; The Javascript engine is V8, and as the name implies, the Javascript engine’s job is, of course, to compile and execute Javascript

Node, by contrast, does not need a layout engine. After all, there is no rendering interface, so there is no Window object in Node because there is no Dom or Bom API. Note that there are middle tiers on both sides of the graph, but they are different. The former is based on the browser sandbox, while the latter is based on Libuv, and the IO read and write capabilities are different. For example, it is easy to read and write disk files on Node, but in the browser, it is difficult to live

As for V8, it’s beyond me to go into detail, but V8 is an open source Javascript engine that you can easily use in your applications, for example:

#include <v8.h> using namespace v8; Int main(int argc, char *argv[]) {// create HandleScope (on stack) HandleScope handle_scope; // Create a New Context object Persistent<Context> Context = Context::New(); Helloworld Context::Scope context_scope(Context); // Create a string object with the value'Hello, Wrold! ', the string object is evaluated by the JS engine // and the result is'Hello, World! '
    Handle<String> source = String::New("'Hello' + ', World! '"); Handle<Script> Script = Script::Compile(source); Handle <Value> result = script->Run(); Dispose(); // Dispose of context resources context.dispose (); // The conversion result is a String String::AsciiValue ASCII (result);printf("%s\n", *ascii); 

    return 0; 
}Copy the code

The above example of a V8 call written in C++ can be summed up in one sentence: throw the Js code (as a string) into V8 and return the result. This is the simplest V8 call template. Remember, Node is also written in C++ based on V8. Does this give you a little more insight into Node?

4. Analogy to Java VIRTUAL Machines

Those of you who are familiar with Java are familiar with its cross-platform features, thanks to its virtual machine. What is a virtual machine? This, in turn, can be understood through the Node environment

There was no Windows version of Node until July 2011, when It was released for Windows with the help of Microsoft, making it cross-platform. Compatibility with Windows and * Nix platforms is largely due to Node’s ability to build a platform architecture layer between the operating system and the upper modules, known as the middle layer, or Libuv, from Section 3. Libuv is the basic component for many systems to implement cross-platform. If you are interested, you can learn about it (I don’t know exactly).

Returning to the question of what is a virtual machine, is it just a set of IO Bridges between the system and the upper modules

Five, the summary

Don’t set boundaries to your area of knowledge

Feimai front end is a knowledge community to let knowledge go deep into the principle, we have knowledge planet, public account and group, welcome to add micro hook up: Facemagic2014