Node.js Note1

Node.js是一款用于构建可扩展互联网应用的软件系统,特别适用于Web服务器。它采用事件驱动及异步I/O的方式减少开销并提高可扩展性。Node.js由Ryan Dahl创建于2009年,主要组件包括Google的V8 JavaScript引擎、libUV和内置库。Node.js的设计受到Ruby的EventMachine和Python的Twisted等项目的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From http://en.wikipedia.org/wiki/Nodejs

Node.js is a software system designed for writing scalable Internet applications, notably web servers.[1] Programs are written in JavaScript, using event-drivenasynchronous I/O to minimize overhead and maximize scalability.[2] Node.js consists of Google's V8 JavaScript engine, libUV, and several built-in libraries.

Node.js was created by Ryan Dahl starting in 2009, and its growth is sponsored by Joyent, his employer.[3][4]

Dahl's original g

 

Similar environments written in other programming languages include Twisted for Python, Perl Object Environment for Perl, lib event for C and EventMachine for Ruby. Unlike most JavaScript programs, it is not executed in a web browser, but is instead a server-side JavaScript application. Node.js implements some Common JS specifications.[6] It provides a REPL environment for interactive testing.

Node.js was selected by InfoWorld for the "Technology of the Year Award" in 2012.[7]

 

From http://nodejs.org/about/

Node's goal is to provide an easy way to build scalable network programs

In the "hello world" web server example below, many client connections can be handled concurrently.

Node tells the operating system (through epollkqueue/dev/poll, or select)  that it should be notified when a new connection is made, and then it goes to sleep. If someone new connects, then it executes the callback. Each connection is only a small heap allocation.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

This is in contrast to today's more common concurrency model where OS threads are employed. 

Thread-based networking is relatively inefficient and very difficult to use. 

See: this and this.

Node will show much better memory efficiency under high-loads than systems which allocate 2mb thread stacks for each connection.

Furthermore, users of Node are free from worries of dead-locking the process—there are no locks.

Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, less-than-expert programmers are able to develop fast systems.

Node is similar in design to and influenced by systems like Ruby's Event Machine or Python's Twisted.

Node takes the event model a bit further—it presents the event loop as a language construct instead of as a library.

In other systems there is always a blocking call to start the event-loop.Typically one defines behavior through callbacks at the beginning of a script and at the end starts a server through a blocking call like EventMachine::run().

In Node there is no such start-the-event-loop call. Node simply enters the event loop after executing the input script.

Node exits the event loop when there are no more callbacks to perform.

This behavior is like browser javascript—the event loop is hidden from the user.

HTTP is a first class protocol in Node.

Node's HTTP library has grown out of the author's experiences developing and working with web servers.

For example, streaming data through most web frameworks is impossible.

Node attempts to correct these problems in its HTTP parser and API.

Coupled with Node's purely evented infrastructure, it makes a good foundation for web libraries or frameworks.

But what about multiple-processor concurrency?

Aren't threads necessary to scale programs to multi-core computers? You can start new processes via child_process.fork() these other processes will be scheduled in parallel. For load balancing incoming connections across multiple processes use the cluster module.

 

 

转载于:https://www.cnblogs.com/sadsheep/archive/2012/05/25/2517850.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值