Introduction and use of node.js and npm

Introduction to node

node.js is a javascript operating environment based on the Chorme V8 engine. For example, the browser is also the operating environment of js, and node is also the operating environment of js, but the application scenario of js code is extended to the server side. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient.

What are the advantages of JavaScript running on Node over other backend development languages?

  JavaScript is single-threaded and can only perform asynchronous IO operations. With the help of JavaScript's natural event-driven mechanism and V8 high-performance engine, it is easy to write high-performance Web services.

  The JavaScript language itself is a complete functional language. In the Node environment, through modularized JavaScript code, plus functional programming, and without considering browser compatibility issues, the latest ECMAScript 6 standard can be used directly, which can fully meet the engineering requirements. demand.

Why use node.js?

  In general, Node.js is suitable for the following scenarios:

  1. Real-time applications, such as online multi-person collaboration tools, web chat applications, etc.
  2. I/O-based high-concurrency applications, such as providing APIs for clients and reading databases.
  3. Streaming applications, such as clients often upload files.
  4. The front and rear ends are separated.

  In fact, the first two can be attributed to one, that is, the client widely uses long connections, although the number of concurrency is high, most of them are idle connections.

  Node.js also has its limitations. It is not suitable for CPU-intensive applications , such as artificial intelligence calculations, video and image processing, and so on.

other concepts

Concepts refer to other articles

concurrency

  Different from the client, one of the data that server developers are very concerned about is the number of concurrency, that is, the maximum number of concurrent client requests that the server can support. The C10K problem in the early years was to discuss how to use a single server to support 10K concurrency. Of course, with the improvement of software and hardware performance, C10K is no longer a problem. We began to try to solve the C10M problem, that is, how a single server can handle millions of concurrency.

  When C10K was proposed, we were still using the Apache server, which works by forking a child process and running the PHP script in the child process whenever a network request arrives. After the script is executed, the result is sent back to the client.

  This can ensure that different processes do not interfere with each other, and even if a process fails, it will not affect the entire server, but the disadvantages are also obvious: a process is a relatively heavy concept, has its own heap and stack, and takes up more memory. There is an upper limit to the number of processes that the server can run, which is about a few thousand.

Although Apache later used FastCGI, it is essentially just a process pool, which reduces the overhead of creating processes, but cannot effectively increase the number of concurrency.

  Java's Servlet uses a thread pool, that is, each Servlet runs on a thread. Although threads are lighter than processes, they are also relative. Some people have tested that the size of the exclusive stack for each thread is 1M, which is still not efficient enough. In addition, multi-threaded programming will bring all kinds of troubles, which must be deeply understood by programmers.

  If you don't use threads, there are two other solutions, using coroutines and non-blocking I/O. Coroutines are lighter than threads. Multiple coroutines can run in the same thread and be scheduled by the programmer. This technique is widely used in the Go language. Non-blocking I/O is used by Node.js to handle high concurrency scenarios.

non-blocking I/O

  The I/O mentioned here can be divided into two types: network I/O and file I/O. In fact, the two are highly similar. I/O can be divided into two steps. First, the content in the file (network) is copied to the buffer, which is located in the memory area exclusive to the operating system. Then copy the contents of the buffer to the memory area of ​​the user program.

  For blocking I/O, from initiating a read request, to the buffer being ready, and then to the user process obtaining data, these two steps are blocked.

  Non-blocking I/O actually polls the kernel to see if the buffer is ready, and continues to perform other operations if not. When the buffer is ready, the content of the buffer is copied to the user process. This step is actually blocked.

I/O multiplexing technology refers to using a single thread to process multiple network I/Os. The select and epoll we often say are functions used to poll all sockets. For example, Apache uses the former, while Nginx and Node.js use the latter, the difference is that the latter is more efficient. Since I/O multiplexing is actually single-threaded polling, it is also a non-blocking I/O solution.

  Asynchronous I/O is the most ideal I/O model, but unfortunately, true asynchronous I/O does not exist. AIO on Linux passes data through signals and callbacks, but it has flaws. Existing libeio and IOCP on Windows essentially use thread pool and blocking I/O to simulate asynchronous I/O.

Network I/O

  Node.js can indeed handle a large number of concurrent requests in a single thread, but this requires certain programming skills. In short, when programming with Node.js, any time-consuming operation must be done asynchronously to avoid blocking the current function. Because you're serving clients, all code is always single-threaded and executed sequentially.

Document I/O

  Asynchrony is to optimize the experience and avoid lag. But to really save processing time and take advantage of CPU multi-core performance, it still depends on multi-threaded parallel processing.

  In fact, Node.js maintains a thread pool at the bottom. As mentioned in the basic concept section before, there is no real asynchronous file I/O, which is usually simulated by thread pool. By default, there are four threads in the thread pool for file I/O.

  It should be noted that we cannot directly manipulate the underlying thread pools, and actually do not need to care about their existence. The function of the thread pool is only to complete I/O operations, not to perform CPU-intensive operations, such as image, video processing, and large-scale calculations.

  If there are a small number of CPU-intensive tasks to be processed, we can start multiple Node.js processes and use the IPC mechanism for inter-process communication, or call external C++/Java programs. If you have lots of CPU-intensive tasks, it can only mean that choosing Node.js was a bad decision.

event loop

  There is an event loop (Event Loop) in Node.js.

  A complete Event Loop can also be divided into multiple phases, followed by poll, check, close callbacks, timers, I/O callbacks, and Idle.

Since Node.js is event-driven, the callback function of each event will be registered in different stages of the Event Loop. For example, the callback function of fs.readFile is added to I/O callbacks, the callback of setImmediate is added after the poll phase of the next Loop ends, and the callback of process.nextTick() is added after the end of the current phase and before the start of the next phase .

  The callbacks of different asynchronous methods will be executed in different phases. It is very important to grasp this, otherwise logic errors will occur due to the order of calls.

  The Event Loop loops continuously, and in each stage, all callback functions registered in that stage will be executed synchronously. That's why I mentioned in the network I/O section, don't call blocking methods in the callback function, always use asynchronous thinking to perform time-consuming operations. A callback function that takes too long may cause the Event Loop to be stuck in a certain stage for a long time, and new network requests cannot be responded in time.

Since the purpose of this article is to have a preliminary and comprehensive understanding of Node.js. I won't introduce each stage of the Event Loop in detail.

data flow

  The benefits of using data streams are obvious, and there are real portrayals in life. For example, the teacher assigns summer homework. If students do a little (homework flow) every day, they can complete the task relatively easily. If the backlog is piled up together, on the last day, facing the pile of homework books, you will feel powerless.

  The same is true for server development, assuming that users upload 1G files, or read local 1G files. If there is no concept of data flow, we need to open up a buffer of 1G size, and then concentrate on processing once the buffer is full.

If the method of data flow is adopted, we can define a very small buffer, for example, the size is 1Mb. When the buffer is full, the callback function is executed to process this small piece of data, thereby avoiding a backlog.

In fact, the file reading of request and fs modules is a readable data stream.

Summarize

  For high-concurrency long connections, the event-driven model is much lighter than threads, and multiple Node.js processes can be easily expanded with load balancing. So Node.js is ideal for serving I/O-intensive applications. But the disadvantage of this approach is that it is not good at handling CPU-intensive tasks.

Construction of node operating environment

node installation

Node installation address: Download | Node.js Chinese Network

You can choose the Node.js version installation package you need according to different platform systems.

 After downloading, install the program, all the way to default, pay attention to the installation directory can not be Chinese

To test whether node is successfully installed, you can use the win+R shortcut key to enter cmd or powershell in win10

In the cmd tool, enter node -v to view the node version. If the version number can be displayed, the node installation is successful.

When multiple projects are developed at the same time, due to different project times, we may encounter inconsistent node.js versions supported  by different projects , resulting in conflicts when running projects. You can use the gnvm-node.js multi-version manager to freely switch and manage the different versions of node we need. For the introduction of gnvm, please check the link below. gnvm-node.js multi-version manager_Progress every day blog-CSDN blog_gnvm

npm

After installing node, we can use npm. The full name of npm is Node Package Manager, which is a node.js package management and distribution tool and the default package manager of Node.js JavaScript runtime environment. It has become an unofficial A standard for publishing Node modules (packages), used for the release, propagation, and dependency control of Node.js packages. npm provides command-line tools that allow you to easily download, install, upgrade, and delete packages, and also allows you to publish and maintain packages as a developer.

npm function introduction

npm is a package management tool installed with Node.js, which can solve many problems in Node.js code deployment. The common usage scenarios are as follows:

    Allows users to download third-party packages written by others from the npm server for local use.
    Allows users to download and install command-line programs written by others from the npm server for local use.
    Allow users to upload their own packages or command-line programs to the npm server for others to use.

Common npm commands

  • npm -v: View the npm version.
  • npm init: A package.json configuration file will appear after initialization. You can add -y to the back to quickly skip the question-and-answer interface.
  • npm install: It will automatically download all dependencies required by the project according to the package.json file in the project.
  • npm install package name --save-dev(npm install package name-D): The installed package is only used in the development environment, not in the production environment, and will appear in the devDependencies attribute in the package.json file.
  • npm install package name --save(npm install package name-S): The installed package needs to be released to the production environment, and will appear in the dependencies property in the package.json file.
  • npm list: View the installed node packages in the current directory.
  • npm list -g: View the node packages that have been installed globally.
  • npm --help: View the npm help command.
  • npm update package name: update the specified package.
  • npm uninstall package name: uninstall the specified package.
  • npm config list: View configuration information.
  • Npm specified command --help: View the help of the specified command.
  • npm info Specify the package name: View all version information of the specified package on the remote npm.
  • npm config set registry https://registry.npm.taobao.org: Modify the package download source. In this example, modify it to Taobao mirror.
  • npm root: View the installation path of the current package.
  • npm root -g: View the installation path of the global package.
  • npm ls package name: View the specified package and version information installed locally, empty is not displayed.
  • npm ls package name -g: View the specified package and version information installed globally, empty is not displayed.

cnpm

Because the npm installation plug-in is downloaded from a foreign server, it is greatly affected by the network. The Taobao npm mirror is a complete npmjs.org mirror. You can use this instead of the official version (read-only). The synchronization frequency is currently once every 10 minutes to ensure as much as possible Sync with official services.

You can use Taobao's customized cnpm (gzip compression support) command-line tool instead of the default npm:

npm install -g cnpm --registry=https://registry.npm.taobao.org

After the installation is complete, enter cnpm -v to check whether the installation is successful 

When using the command, replace npm with cnpm

Guess you like

Origin blog.csdn.net/m0_53206841/article/details/125822044