Node.js that makes me addicted

Node.js that makes me addicted

 

Since I came into contact with the first programming language C in my freshman year, I have been in contact with C, assembly, C++, C#, Java, JavaScript, PHP, and some HTML, CSS magic horses, and have never had a language for three years. Got me as obsessed with node.js. When I contacted nodejs, I mentioned in my previous blog that many people recommended the nodejs version when choosing a server for web sockets. Unfortunately, I didn’t have any concept of nodejs at that time, so I didn’t understand the code written by others, but I felt very familiar. Using javascript running on the server side to explain what nodejs is, this made me even more puzzled, how can javascript be a socket server. After writing the server in C#, I looked back at node.js. After ten minutes, I was completely attracted.

Supplementary note: Since blogging is in the stage of understanding the grammar of nodejs, many questions are not clearly explained, and there are many people who oppose it. For some principled things, you can see the misunderstandings about node.js

what is node.js

What exactly is node.js? Take a look at the description of nodejs on the official website :

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

We can conclude a few points about this

  1. node.js is a platform built on the Chrome JavaScript runtime environment, this is a very important point, node.js is not a language, but a platform
  2. node.js is dedicated to making it easier to build fast, stable network programs
  3. Node.js features event-driven and non-blocking I/O, making it lightweight and efficient
  4. node.js is great for running data-intensive real-time applications on distributed devices

JavaScript running on the server side?

Chrome JavaScript runtime is also known as Chrome's V8 JavaScript Engine, which is a low-level JavaScript engine developed by Goole for Chrome browser. It is used to parse JavaScript statements to create its runtime environment and ensure that the statements we write are on the browser. performance was in line with our expectations.

So why is node.js said to be JavaScript running on the server side? Why should nodejs be related to V8? In addition to Google's V8 interpretation of JavaScript is very fast, a very important reason is that the V8 JavaScript engine is not limited to running in the browser, can be embedded in any application to run. Node.js  is similar to the .net framework as a platform (some are confused here, I hope the gods give some pointers), but it does not create a language like .net - C# runs on this platform, but it is very It cleverly borrows the JavaScript syntax that web developers are already very familiar with, uses the V8 engine to parse the statement, and rebuilds it for use on the server. So strictly speaking node.js is not Javascript running on the server side, but a platform that can run JavaScript syntax on the server side. 

Why use node.js

After a long time, it's a new bottle of old wine. It seems that except for a fresh JavaScript syntax, node.js has no advantages, why use it instead of Java or C# that can also run on the server side? This starts with the features of node.js event-driven and non-blocking I/O. Students who are familiar with JavaScript about event driving should be familiar with it. Node.js uses a series of "non-blocking" libraries to support the way of event loop. In essence, it provides interfaces for resources such as file systems and databases, such as a database access, using an event mechanism. After initiating a request, the process is immediately handed over. When the data is returned, an event is triggered, and the data continues to be processed.

In traditional blocking I/O it works like this

int num=query('select * from ......');

print(num);

......//Irrelevant statement

The print method must wait for the query method to return the result. If there is a network connection failure in the database, the print method must wait until the timeout to execute, and then some unrelated statements can be executed at one time, and this is the case in non-blocking I/O

copy code
query('select * from...',callback(data){
  ............. // Related statement 
  print();
});

.............. // irrelevant statement
copy code

Use ajax with us to process the result in the callback function, but it does not affect the execution of the following statement. No wonder node.js borrows JavaScript to do this, JavaScript has several features that make it well-suited for the job

  1. event mechanism
  2. Functional programming, support for anonymous functions, function parameters
The code in Node.js is executed in a single process and a single thread (the code we wrote is, but students who are not interested in node.js itself can look at this for further understanding), using the event polling mechanism and non-blocking I/O, in Parallelize tasks without adding additional threads. Node.js solves the problem that blocking programming wastes a lot of process resources just waiting, resulting in a lot of memory and cpu waste, so it dares to claim that it is perfect for data-intensive real-time web applications.

how to install

The installation of nodejs is still very simple, especially now that the installation package is integrated with npm, there is no need to install it separately, just download the installation package and run it. In addition, if you want to use nodejs completely, you need a C language compilation environment, git and Python, students who use windows are likely to be a little more troublesome, and they need to get these things done. Students who have a soft spot for the command line can take a look at How to Install Node.js

what is npm

npm is the abbreviation of node packaged modules, which is actually a management tool for nodejs modules

what is module

The concept of the so-called module is very similar to that of the package in java. For a collection of solutions, the official will provide a few core ones, and many third-party ones.

How to install third-party modules

Because the nodejs community is very active, there are many useful third-party packages, we can use the npm command to install in the terminal

  • npm install [-g] <name>: Use the install command to download and install nodejs at the global path of nodule, otherwise it will be installed to the current path
  • npm remove <name>:移除module
  • npm update <name>:更新 module

There are also some common commands, you can see npm common commands

the first demo

 After all these are done, we can write the legendary hello world. Create a test.js file in any directory

console.log('Hello, World!');

A simple sentence is enough, and then use the node command to run

 This is this. . . Too untechnical, look at the official demo, how easy it is to create a web server with nodejs

copy code
var http=require('http'); // Introduce http module 
  2 http.createServer( function (request,response){ // Create a web server 
  3      // Callback function, so that creating server method will not block 
  4 response .writeHead(200,{'contentType':'text/plain' });
   5 response.end('Hello World!\n' );
   6 }).listen(8124 );
   7 console.log('Server running at http://127.0.0.1:8124/');
copy code

 Run the server first, press Ctrl+C twice to exit

Use a browser to visit

It's that simple

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327106540&siteId=291194637