What is node. js?
node. JS is written in the C + + language, and the browser kernel V8 as the execution engine; node is not a JS application, but a JavaScript run environment. Node retains the interface of the front-end browser JS, without rewriting any of the features of the language itself, still based on the scope and prototype chain.
node. JS is a platform created for real-time web (real-time web) application development, which has taken into account the scalability of the architecture in real-time response and hyper-scale data requirements from the very beginning. This makes it abandon the traditional platform relies on multithreading to achieve high concurrency design ideas, and the use of single-threaded, asynchronous I/O, event-driven program design model. These features not only bring significant performance gains, but also reduce the complexity of multi-threaded programming and thus improve development efficiency.
Features of node. js
1. A JavaScript Runtime environment
2, rely on the chrome V8 engine for code interpretation
3. Event-driven
4. Non-blocking I/O
5. Light weight, scalable, suitable for real-time data interactive application
6, single process, single thread
What can node. js do?
1. Web sites with complex logic
2. Large-scale WEB applications based on social networks;
3. Web Socket Server (page tour, Web IM);
4, TCP/UDP socket application;
5, command line tools;
6, interactive terminal program;
7. Local application with graphical user interface;
8, Unit testing tools;
9. Client-side JavaScript compiler
node. JS Schema
Event Loops
Nodejs execution Model: Single thread event Loop
When the application request occurs, the first entry into the V8 engine, and then into the event queue, can be understood as they are constantly in the loop, to see if there is a task, to execute the task. is a single-threaded model.
Npm
Backend in the development of other languages, there are some concepts of the module or a third party provides a very useful small module. Similarly, node. JS also had a warehouse when it came out. This warehouse is a module dedicated to managing the contributions of Chinese developers, and it is developing very quickly. Similarly, the front-end has some foot hand pieces, run on the server side of the Debug,express,express-session,thrift, relying on this plugin to do thrift related things, images other some of the plug-ins you want to, are can be found from it.
Architecture architecture
is a push part of the Web platform architecture system, a set of cloud group into the resources, through Nginx as a distribution, node can have multiple nodes, through the session into. Every node has a pattern, I believe that everyone in the deployment of the deployment of a node, the deployment of a node, or the node hangs is hanging.
Thrift use
Defining interfaces
2, compile, generate the corresponding package, and upload to the corresponding library
3. Use in node, as follows:
Note: There's a hole here.
A basic type in thrift is called a double (64-bit floating-point number). When defined as this type, data from Java to Node becomes all 0.
Solution Solutions
is defined as a string type, which is then converted to a floating-point number at node, if necessary, or directly to the page.
IP payload (IP load Balance)
Load Balancing
Distributed across multiple operational units, such as Web servers, FTP servers, enterprise critical application servers, and other mission-critical servers, to work together to accomplish tasks.
There is no mechanism for load balancing in native, but we can use dynamic proxy design mode, based on thrift client, using JS prototype chain to achieve.
There are many ways of balancing, and we use a polling mechanism to implement access to multiple Java nodes.
Session Management
Node itself does not have a session mechanism, we can use the Express-session package to implement, while using Redis to store the session.
Connection pool
Traditional Read database mode:
What does a connection pool need to do?
1. Connection preheating (automatic opening of n connections for use at startup)
2. Distribute the connection request evenly using the rotation method, for example
3. When the connection in the pool is about to run out, a new connection is generated dynamically
4. When the connection in the pool is not invoked for a period of time, the connection is automatically released
5, automatically discard the broken connection
6. Automatically release all connections when the system shuts down
Based on this, we can also use a few plug-in packages (such as Generic-pool (Node-pool), Node-thrift-pool, of course, if you directly node connected to the DB, basically the package will also support the connection pool) in node using the connection pool.
Using the example
What you can do with node
1, do some gray areas of things
Node can do some gray-area things, because it has the advantage of the front-end, can be asynchronous, to initiate asynchronous requests. Bring a lot of benefits to developers. However, you need to manage your type. If the type itself is not managed well, there will be some problems. At the same time it can also do some back-end things. such as connection pool and so on.
2. More distinct modules
3, can be easily after the division of labor
Data from the browser, through node to transform this layer of data into Java needs a data structure, can make the division of labor more clear.
4. Common form Input Verification
If you are writing a system, for security reasons, regardless of how much verification is done on this side of the browser, you have to do input validation. Traditional mode is the need to write a Java classmate, the front-end students to write a copy. Because node and Java are both deployed in a server cluster or an area, you can trust a data between the two sides. Verification from the browser, you can share the form input verification, to achieve cost-saving purposes.
The above is from a push Web service chief architect Jang Titing on March 12 Segmentfault D-Day Beijing: Back-end speech organized.
Five best cases to explain the front and back of node. js