What problems can 1.node.js solve?
node. JS does not create a new thread for each client connection, but instead triggers an event that is processed inside node. js for each client connection. Therefore, the efficiency of high concurrency access can be solved.
"Analysis" in a server-side language such as java/php, a thread is created for each client request, each thread consumes a maximum of 4,000 2m,8g of memory, and node. JS can handle connections of up to tens of thousands of clients at a time. Therefore, you should consider using node. js When you need to use a Web application to support concurrent connections for a large number of users.
2. Two mechanisms for high performance for node. JS: Non-blocking I/O and event loops.
Non-blocking I/O: Traditional single-threaded access to the database when the entire thread pauses to wait for the result to return, and node. JS executes the code that accesses the database and immediately executes the subsequent code, putting the processing code of the returned result in the callback function, thus improving program execution efficiency.
Event Loop: Only one event callback function can be executed at a time, but in the middle it is possible to handle other events, including the departure new event, the life of the event's callback function, and then return the callback function to continue executing the original event.
3.node.js Applications for Development:
Chat server: High concurrent connection, low complexity processing;
A comprehensive service website or e-commerce website: receives large amounts of data per second and writes it to the buffer, and then extracts it from the cache and writes it to the database by processing it separately.
Modules in the 4.node.js:
node. JS is functionally partitioned in modules, one JS file per module. The scope of the global variable or function defined in each module is also scoped to the module, and only the exports object can be used to pass it to the outside.
function { return "Foo"; }; var foo = require ('./foo.js ');
Foo.printfoo (); Calling a method in a module
The "understanding" exports is similar to the keyword public in Java, and can only be called externally when a variable or function is defined and modified by it.
Some core modules in node. js
| Assert |
Unit Test Assertion Processing |
| Buffer |
Processing and conversion of binary data |
| Console |
For console output information |
| Fs |
For manipulating files and file systems |
| http |
For implementing HTTP servers and clients |
| Https |
Used to create HTTPS servers and clients |
| Net |
Used to create a TCP server and client |
| Os |
Used to obtain operating system information |
| Path |
Used to process file paths |
| ReadLine |
Used to read a row of standard input streams |
Url |
For parsing and formatting URL strings |
| Util |
Used to implement various utility functions |
| Zlib |
Internal practical Zlib Class Library to achieve data compression and decompression processing |
Classes, objects, functions that can be used directly
| Buffer class |
Used to provide buffers for the storage of binary data |
| SetTimeOut () |
Used to execute the specified function (milliseconds) when the specified time arrives |
| Cleartimeout () |
Cancel settimeout () execution |
| SetInterval () |
Perform a specified function once per number of milliseconds |
| Clearinterval () |
Cancel SetInterval () execution |
| Require () |
For loading modules |
| Module Object |
Used to access module information |
| Process Object |
Used to access process information |
Note: You can also install third-party modules for feature extensions, such as template engines, database access, and so on.
node. JS Overview