Node. JS: a "code ready" Server

Source: Internet
Author: User
Tags php server node server



Node is a server.
Javascript interpreter, which will change the concept of how the server should work. It aims to help programmers build highly scalable applications and write programs that can process tens of thousands of concurrent connections to one
There is a connection code for the physical machine. This article explores what problems node. js can solve, how it works, how it runs a simple application, and when node is not a good one.

What is the purpose of node?

The goal publicly stated by node is "to provide a simple method for building scalable network programs ". What is the problem with the current server program? Let's make a math. In Java and PHP
In the class language, each connection generates a new thread, and each new thread may require 2 MB of supporting memory. In
Theoretically, the maximum number of concurrent connections in a ram system is 4,000. As your client base grows, you want your web application to support more users. In this way, you must add more
Server. Of course, this will increase business costs, especially server, transportation, and labor costs. In addition to these increases in costs, there is also a technical problem: users may use different servers for each request,
Therefore, any shared resource must be shared among all servers. For example, in Java, static variables and caches need to be shared between JVMs on each server. This is the entire web application architecture.
Bottleneck in the architecture: the maximum number of concurrent connections that a server can process.

Node can solve this problem by changing the connection method to the server. Each connection creates a process. The process does not need to support memory blocks, instead of generating a new OS for each connection.
Thread (and allocate some supporting memory to it ). Node claims that it will never be deadlocked because it does not allow the use of locks at all, and it will not directly block I/O calls. Node also claims that the server running it can support
Tens of thousands of concurrent connections. In fact, node changes the server landscape by changing the bottleneck in the entire system from the maximum number of connections to the traffic of a single system.

Now that you have a program that can process tens of thousands of concurrent connections, what can you build with node? If you have a web application that needs to process so many connections
"Terror" thing! It is a "if you have this problem, it is not a problem at all"
. Before answering the above questions, let's first look at how node works and how it is designed to run.

Node is definitely not

Yes, node is a server program. However, it is certainly not like Apache or tomcat. Those servers are independent server products and can immediately install and deploy applications. Through this
You can start and run a server within one minute. Node is definitely not such a product. Apache can add a PHP module to allow developers to create dynamic web pages.
Tomcat programmers can deploy JSPs to create dynamic web pages. Node is definitely not of this type.

In the early stages of node (currently version 0.4.6), it is not a "ready" server program. You cannot install it and place files in it, has a fully functional web server. Even if you want to enable and run this basic function after the Web server is installed, you still need to do a lot of work.

How node works

Node itself runs V8 JavaScript. Wait, the JavaScript on the server? No, you are not mistaken. Server-side Javascript is a relatively new concept that was mentioned about two years ago when discussing the Aptana jaxer product on developerworks (see references)
). Although jaxer has never been really popular, this concept is not out of reach-Why cannot I use the programming language on the client on the server?

What makes V8? V8
The JavaScript engine is the underlying JavaScript Engine that Google uses for their Chrome browser. Few people think about the actual JavaScript on the client.
What have you done? In fact, the JavaScript engine is responsible for interpreting and executing code. Using V8, Google creates an ultra-fast interpreter written in C ++, which has another unique
Feature. You can download the engine and embed it into any application. It is not limited to running in a browser. Therefore, node actually uses Google's V8
The JavaScript engine is rebuilt to be used on the server. Perfect! Since there is already a good solution available, why should we create a new language?

What are the benefits of node?

So far, you should be able to answer "What is node"
This is a problem, but you may not know when to use it. This is an important question to be raised, because node is good for something, but on the contrary, for other things, currently
Node may not be a good solution. You need to be careful when to use node, because using it in case of errors may lead to a redundant lot.

As you have seen before, node is very suitable for the following situations: you may expect high traffic, and the server logic and processing need not be huge before responding to the client. Typical examples of outstanding node performance include:

  • Restful API

Provides restful
The API Web Service receives several parameters, parses them, combines a response, and returns a response (usually less text) to the user. This is ideal for node because you can build it
Processes tens of thousands of connections. It does not need a lot of logic; it just looks for some values from a database and combines a response. Because the response is a small amount of text, and a small amount of text is used for inbound requests, the traffic is not high, and a machine is very
It can also handle the API requirements of the busiest companies.

  • Twitter queue

Imagine a company like Twitter that must receive tweets and write it into a database. In fact, there are almost thousands of Tweets per second
The database cannot handle the number of writes required during peak hours in a timely manner. Node becomes an important part of the solution to this problem. As you can see, node can process tens of thousands of inbound tweets. It can
Quickly and easily write them into a memory queuing system (for example
Memcached), another independent process can write them into the database from there. Node's role here is to quickly collect tweet and pass this information to another write process.
Imagine another design-a common PHP server tries to write data to the database itself-
Each tweet will cause a short delay when writing data to the database because the database call is blocking the channel. Due to database latency, a machine designed in this way can only process 2000 entries per second.
Site tweets. 1 million servers are required for 500 Tweets per second. On the contrary, node can process each connection without blocking the channel, so as to capture as many tweets as possible. One
A node machine that can process 50,000 pieces of tweets only needs 20 servers.

  • Image File Server

A company with a large distributed website (such as Facebook or
You may decide to use all machines only for service images. Node will be a good solution to this problem, because the company can use it to write a simple file searcher and then process
Tens of thousands of connections. Node will find the image file, return the file or a 404 error, and then do nothing. This setting will allow such distributed websites to reduce their service images,. js and
The number of servers required for. CSS files and other static files.

What are its disadvantages?

Of course, in some cases, node is not an ideal choice. The following are areas that node is not good:

  • Dynamically created page

Currently, node does not provide a default method to create dynamic pages. For example, when using the assumerver pages (JSP) technology, you can create
A JSP code segment such as <% for (INT I = 0; I <20; I ++) {}%> contains the cyclic index. jsp
Page. Node does not support such dynamic HTML-driven pages. Similarly, node is not suitable for Web servers such as Apache and tomcat. Therefore, if you want
To provide such a server-side solution, you must write the entire solution yourself. PHP programmers do not want to write a PHP converter for Apache every time they deploy a web application.
So far, this is exactly what node requires you to do.

  • Relational Database Service (RDS) Heavy-duty applications

Node aims to be fast, asynchronous, and non-blocking. Databases do not necessarily share these goals. They are synchronous and congested, because the database call during read/write will be blocked until the result is generated. Therefore,
A Web application that requires a large number of database calls, a large number of reads, and a large number of writes for each request is not suitable for node, because the relational database itself can offset many advantages of node. (New
Nosql databases are more suitable for node, but they are completely another topic .)


The question is, "What is node. js ?" The answer should be answered.
After reading this article, you should be able to answer this question in a few clear and concise sentences.
Problem. If so, you have already taken the lead of many coders and programmers. I have talked with many people about node, But they
Node is always confused about what it is. It is understandable that they have some ways of thinking in Apache-
The server is an application that puts HTML files into it, and everything will run normally. Node is the target driver. It is a software program that uses JavaScript to allow lightweight programmers
Allows you to quickly create a fast and scalable Web server. Apache is running, while ode is encoded.

Node achieves its goal of providing highly scalable servers. It does not allocate a "every connection and one thread" model, but uses a "every connection and one process" model"
Model. Only the memory required for each connection is created. It uses Google's extremely fast JavaScript Engine: V8. It uses an event-driven design to keep the code minimal and easy
Read. All of these factors contribute to the ideal goal of node-it is easier to write a highly scalable solution.

Just as important as understanding what node is, understanding it is not. Node is not a substitute for Apache, which aims to make PHP Web applications more scalable. This is true. In this initial stage of node, a large number of programmers are unlikely to use it, but it performs very well in scenarios where it can play a role.

What should I expect from node in the future? This may be the most important question in this article. Now that you know what it is, you should want to know what it will do next. In the next year
In, I look forward to better integrating node with the existing third-party support library. Now, many third-party programmers have developed plug-ins for node, including adding file server support and MySQL support
Hold. It is expected that node will start to integrate them into its core functions. Finally, I want
Node supports a dynamic page module, so that you can perform operations in PHP and JSP (maybe an NSP, a node Server Page) in the HTML file. Finally, Xi
It is expected that one day there will be a "ready for deployment"
You can download and install the node server, just put your HTML file in it, just like using Apache or tomcat. Node is still in its initial stage, but it has developed
Soon, it may appear in your field of view soon.

This article is based on the developerworks author Mike abernethy.
At michael
In his 13-year technical career, abernethy has been dealing with a variety of different technologies and clients. He is currently a free programmer and is good at Java high availability and jquery. He is now focused on rich
Internet applications, trying to achieve the complexity and simplicity of applications at the same time. He often plays golf in his spare time. More specifically, he finds him playing a golf ball in the bush.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.