Node.js是什么?

来源:互联网 发布:mac ae 2017改中文版 编辑:程序博客网 时间:2024/05/21 10:45

Node.js是一个服务器端程序,运程V8 Javascript,也就是服务器端的JavaScript。

传统服务器(如Apache, Tomcat) 每接受一个连接,将为其分配一个线程。这样每台服务器同时处理的连接就受到很大的限制,假如一台服务器内存为8GB,一个线程分配2M,那为同时处理的连接数大约为4000左右。

Node.js改变了这种连接方式,而使用了事件驱动。新的连接到来时会在Node引擎中注册一个事件,而不是创建一个新的线程。Node.js没有锁的概念,所以永远不会死锁,同时可以支持数万个连接。


What problem does Node solve?

Node's stated number one goal is "to provide an easy way to build scalable network programs". What's the issue with current server programs? Let's do the math. In languages like Java™ and PHP, each connection spawns a new thread that potentially has an accompanying 2 MB of memory with it. On a system that has 8 GB of RAM, that puts the theoretical maximum number of concurrent connections at about 4,000 users. As your client-base grew, if you wanted your web application to support more users, you had to add more and more servers. Of course, this adds to a business's server costs, traffic costs, labor costs, and more. Adding to those costs are the potential technical issues — a user can be using different servers for each request, so any shared resources have to be shared across all the servers. For all these reasons, the bottleneck in the entire web application architecture (including traffic throughput, processor speed, and memory speed) was the maximum number of concurrent connections a server could handle.

Node solves this issue by changing how a connection is made to the server. Instead of spawning a new OS thread for each connection (and allocating the accompanying memory with it), each connection fires an event run within the Node engine's process. Node also claims that it will never deadlock, since there are no locks allowed, and it doesn't directly block for I/O calls. Node claims that a server running it can support tens of thousands of concurrent connections.

So, now that you have a program that can handle tens of thousands of concurrent connections, what can you actually build with Node? It would be awesome if you had a web application that required this many connections. That's one of those "if you have this problem, it's not a problem" kind of problems. Before we get to that, let's look at how Node works and how it's designed to run.

http://www.ibm.com/developerworks/opensource/library/os-nodejs/#N10084





原创粉丝点击