preface

This article is the beginning of the full stack road Node series of articles. Since the launch of Node in 2009, after 11 years of development, Node momentum is more and more fierce, various frameworks such as Express, Koa emerge in an endless stream, the ecology is more and more perfect. Because of JS, Node has become a shortcut for front-end engineers to get to the full stack. Today we will take a look at Node through nine questions.

What is Node.js?

Node.js is a Javascript runtime environment based on Chrome V8 engine developed by Google. This runtime environment includes the compiler (interpreter) needed to run the code and the underlying support of the operating system.

What can Node.js be used for?

With Node.js you can write system – or server-side code using JavaScript. One might ask why not before Node? That’s because JavaScript was originally designed as a front-end language. It doesn’t have an interface for system calls such as file reading and writing. Back-end systems are basically file reading and writing, so you can’t write back-end systems before Node.js.

What are the features of Node.js?

  • Asynchronous I/O
  • Event-driven and callback functions
  • Single thread
  • cross-platform

What are the advantages of Node.js?

Node.js is good at I/O intensive scenarios, which is one of its strengths. Node is network-oriented and good at handling parallel I/O. This is mainly due to Node’s ability to handle event loops, rather than starting a thread for each request service, which consumes very little resources.

What is event-driven?

On an interactive user page, the user will generate a series of events, including clicking buttons, dragging elements, and so on. These events will be loaded in a queue in order. In addition to the page event, there are also some AjAx successful execution, file read completed events. Node.js is event-driven and provides most of its apis in an event-based, asynchronous style. Take the Net module as an example, where only the events of the net.socket object include connect, data, timeout, etc. Developers using Node.js need to register the corresponding callback function with their own business logic.

What is the difference between synchronous and asynchronous?

Synchronous and asynchronous thread/process is described the invocation of the way, a synchronous invocation is refers to the process/thread initiated after the call, have been waiting for the call returns after continue to perform the operation, but does not mean that the CPU to stop working, the CPU will then switch to another thread to continue to wait until the call returns and then switch back to the original process/thread. Asynchrony is just the opposite. When a call is made, it continues down, and when the call returns, it notifies the caller by some means.

What is the difference between blocking IO and non-blocking IO?

In Node, IO refers specifically to the process by which Node programs interact with system disks and networks, supported by Libuv. IO is usually divided into two parts, one is the data preparation phase, and the other is the return result. After a process initiates a call, the blocking IO waits for the two phases to complete, and then waits for the return result to run again. Non-blocking IO is similar to the above procedure, except that if the data has not been continued, a result is immediately returned telling the process that it is not ready.

What are the advantages and disadvantages of single threading?

Node maintains the single-threaded nature of JavaScript in the browser, and no state is shared between JavaScript and other threads in Node. Its biggest advantages are that it does not care about synchronization, there are no deadlocks, and there is no overhead associated with thread switching. The disadvantages of single-threading are that you can’t take advantage of a multi-core CPU, errors can cause the entire application to exit, application robustness is worth considering, and you can’t continue to call asynchronous methods because of the amount of computing that consumes the CPU.

What are the disadvantages of Node.js?

The drawback of Node is that debugging is difficult. Because there’s no stack Trace, it’s hard to find the cause of the problem. If poorly designed, it’s easy to fill your code with callbacks.

This article is formatted using MDNICE