帮助认识理解NODE的经典

本文介绍了Node.js的基本概念,包括其作为命令行工具的安装与使用,以及如何通过V8引擎运行JavaScript程序。Node.js提供了网络和文件操作的API,并采用单线程事件循环机制实现高效的并发处理。

原帖: http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb

在我给别人介绍Node.js的时候通常会给人留下两种印象。有的立刻就“懂了”,有的却感到非常非常的晕。

如果你刚好属于第二种,在这里就是我想尝试解释的一下:

- 它是一个命令行工具,你下载tar文件,编译再安装源文件

- 在你的命令窗口里,键入“node my_app.js"就可以运行javascript程序

- Javascript程序是在V8引擎(就是那个让google chrome速度这么快的东西)上运行的

- Node提供网络和文件的API接口


“但是我用:Ruby, python, php, java,..."都可以实现我想要的一切!”

我懂你的意思。你没错!不好意思,Node并不是一个牛逼货,它无法来帮你做需要你做的事情。它就是一个工具,而且它很可能不能完全代替你的其他工具,至少现在不行。

"那你到底神马意思?"

好吧。Node主要就是在你需要同时做很多事情的时候很有效。你还记不记得你写代码的时候想要”并行运行”?那么Node就是让所有的任务都并行运行,除了你的代码。

"你到底在是说神马?"

That's right, everything runs in parallel, except your code. To understand that, imagine your code is the king, and node is his army of servants.

我就是说,除了你的代码,其他的东西都是并行运行的。要理解这一点,先想象你的代码是一个国王,而Node是一队为他工作的奴役。

这天,一个奴隶把国王叫醒然后问他是否需要什么。国王给了他一系列的任务然后又转头回去睡觉。这个奴隶就是开始在他们工作伙伴中分配任务。他们各种领着任务就去做了。

当一个奴隶完成了他手上的任务后,他排在国王的门口准备汇报。国王一次让一个奴隶进来汇报。完事儿后,国王可能又会重新给他安排一些任务。

这日子真是不错。国王的奴隶们同时进行着他们的工作,而每次只有一个人汇报工作,这样国王很容易专注。


"这简直太霸气了, 但是你能不能别装逼了,跟我用程序猿语言交流行么?"

Sure. A simple node program may look like this:

没问题。一个简单的Node程序看起来是这个样子的:

var fs = require('fs')
  , sys = require('sys');

fs.readFile('treasure-chamber-report.txt',function(report){
  sys.puts("oh, look at all my money: "+report);
});

fs.writeFile('letter-to-princess.txt','...', function(){
  sys.puts("can't wait to hear back from her!");
});

Your code gives node the two tasks to read and write a file, and then goes to sleep. Once node has completed a task, the callback for it is fired. But there can only be one callback firing at the same time. Until that callback has finished executing, all other callbacks have to wait in line. In addition to that, there is no guarantee on the order in which the callbacks will fire.

你的代码给了Node两个任务:读取和写入一个文件, 然后你代码又去睡觉了。一旦Node完成了一个任务,它里面的回调函数就开始运行。不过同一时间只能有一个回调函数被激活。在这个回调函数完成任务前,其他所有的函数都得排着队等着。再多说一句,回调函数运行的顺序可是不能保证的。

"所以我不用担心在我代码里会同时使用同样的数据结构?"

你懂了!这就是Javascript最绝妙的设计:单线程和事件循环设计!

"非常好,但是为什么我应该用它呢?"

一个原因是效率。在互联网应用中,最主要的响应事时间通常是运行所有数据库命令所花费的时间总和。有了Node,你可以一次性让所有的命令同时进行,这样响应时间就变成了执行那个最慢的命令所需要的事件。

另外一个原因是Node是Javascript。你可以用Node实现浏览器和你后台程序的代码共享。Javascript也在慢慢的变成真正的通用语言。不管你之前使用过python, ruby, java, php...,你应该多多少少的学过了一些JS,对吗?

And the last reason is raw speed. V8 is constantly pushing the boundaries in being one of the fastest dynamic language interpreters on the planet. I can't think of any other language that is being pushed for speed as aggressively as JavaScript is right now. In addition to that, node's I/O facilities are really light weight, bringing you as close to fully utilizing your system's full I/O capacities as possible.

最后一个原因就是原始速度。V8稳定更新着这个星球上最快的动态语言编译器的速度界限。我想不出另外一个语言能和Javascript一样可以让这个速度加快的如此猛烈。除了这点,Node的I/O设施都和轻量,让你尽可能的使用你系统全部的I/O性能。

"所以你说我从现在开始应该用Node来写所有的应用了呗?"

Yes and no. Once you start to swing the node hammer, everything is obviously going to start looking like a nail. But if you're working on something with a deadline, you might want to base your decision on:

对,也不对。一旦你开始使用Node利器,所有的东西看起来都是小事一件了。不过如果你要赶在一个截止日期完成任务,你最好考虑几点:

- 低响应时间/高同步运行是不是很重要?Node在这方面绝对是天才。

- 你的项目有多大?小项目一般没有问题。大的项目应该仔细衡量(比如你有多少可使用的库,有没有资源来修bug,或者你是否有两个上冲行程。)

"Node 在Windows上面能运行么?"

No. If you are on windows, you need to run a virtual machine (I recommend VirtualBox) with Linux. Windows support for node is planned, but don't hold your breath for the next few months unless you want to help with the port.

不能。如果你在windows环境里,你需要一个Linux虚拟环境(我推荐VitualBox).Windows的系统支持已经计划了,但是不要抱希望于在几个月内可以完成。

*译者注:这篇文章写于2010年,而现在Node在windows环境中已经随便运行了

"我可以在Node中使用DOM吗?"

Excellent question! No, the DOM is a browser thingy, and node's JS engine (V8) is thankfully totally separate from all that mess. However, there are people working on implementingthe DOM as a node module, which may open very exciting possibilities such as unit testing client-side code.

非常好的问题!不行,DOM是浏览器那边的东西,而Node所使用的V8引擎非常好的完全避免了混乱。不过,也有人想要把DOM做成一个Node模块,这个可能会让像客户端的单元测试的可能行大大提高。

"用事件驱动法编程不是很难么?"

That depends on you. If you already learned how to juggle AJAX calls and user events in the browser, getting used to node shouldn't be a problem.

这个取决于你自身。如果你已经学过怎么处理AJAX呼叫和浏览器里的用户事件,那就很容易熟悉Node的编程方式了。

不管怎么编程,用测试驱动开发程序都能帮你很好的设计出可维护的架构。aintainable designs.

"谁在用Node?"

There is a small / incomplete list in the node wiki (scroll to "Companies using Node"). Yahoo is experimenting with node for YUI, Plurk is using it for massive comet and Paul Bakaus (of jQuery UI fame) is building amind-blowing game engine that has some node in the backend.Joyent has hired Ryan Dahl (the creator of node) and heavily sponsors the development.

Oh, and Heroku just announced (experimental ) hosting support for node.js as well.

这里给出一个node wiki (寻找"Companies using Node") 中列出的不完全的目录。Yahoo在用Node评测他们的YUI,Plurk使用它来创建大批的彗星, Paul Bakaus在后台用node制作一个有幻觉效果的游戏引擎。Joyent已经雇佣了Pyan Dahl(Node的创始人),并大力赞助Node开发。

"我可以在哪儿获取更多学习资源?"

Tim Caswell 正在写一个很棒的博客:How To Node. 在twitter上关注 #nodejs. 订阅 mailing list. 或者来IRC频道,#node.js玩儿. 我们马上就会达到200个潜伏用户数量了。

编者注:早达到了。。。

我会继续在debuggable.com上面写文章的。

就这些,有什么问题尽管提问。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值