Understanding node.js by Felix Geisendörfer(翻译 by shangyan)

9/4/10 by Felix Geisendörfer

Node.js has generally caused two reactions in people I‘ve introduced it to. Basically people either "got it" right away, or they ended up being very confused.

If you have been in the second group so far, here is my attempt to explain node:

  • It is a command line tool. You download a tarball, compile and install the source.
  • It let‘s you run JavaScript programs by typing ‘node my_app.js‘ in your terminal.
  • The JS is executed by the V8 javascript engine (the thing that makes Google Chrome so fast).
  • Node provides a JavaScript API to access the network and file system

"But I can do everything I need in: ruby, python, php, java, ... !".

I hear you. And you are right! Node is no freaking unicorn that will come and do your work for you, sorry. It‘s just a tool, and it probably won‘t replace your regular tools completely, at least not for now.

"Get to the point!"

Alright, I will. Node is basically very good when you need to do several things at the same time. Have you ever written a piece of code and said "I wish this would run in parallel"? Well, in node everything runs in parallel, except your code.

"Huh?"

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.

The day starts by one servant waking up the king and asking him if he needs anything. The king gives the servant a list of tasks and goes back to sleep a little longer. The servant now distributes those tasks among his colleagues and they get to work.

Once a servant finishes a task, he lines up outside the kings quarter to report. The king lets one servant in at a time, and listens to things he reports. Sometimes the king will give the servant more tasks on the way out.

Life is good, for the king‘s servants carry out all of his tasks in parallel, but only report with one result at a time, so the king can focus. *

"That‘s fantastic, but could you quit the silly metaphor and speak geek to me?"

Sure. A simple node program may look like this:

 1 var fs = require(‘fs‘)
 2   , sys = require(‘sys‘);
 3
 4 fs.readFile(‘treasure-chamber-report.txt‘, function(report) {
 5   sys.puts("oh, look at all my money: "+report);
 6 });
 7
 8 fs.writeFile(‘letter-to-princess.txt‘, ‘...‘, function() {
 9   sys.puts("can‘t wait to hear back from her!");
10 });

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.

"So I don‘t have to worry about code accessing the same data structures at the same time?"

You got it! That‘s the entire beauty of JavaScripts single-threaded / event loop design!

"Very nice, but why should I use it?"

One reason is efficiency. In a web application, your main response time cost is usually the sum of time it takes to execute all your database queries. With node, you can execute all your queries at once, reducing the response time to the duration it takes to execute the slowest query.

Another reason is JavaScript. You can use node to share code between the browser and your backend. JavaScript is also on its way to become a really universal language. No matter if you did python, ruby, java, php, ... in the past, you‘ve probably picked up some JS along the way, right?

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.

"So you are saying I should write all my apps in node from now on?"

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:

  • Are low response times / high concurrency important? Node is really good at that.
  • How big is the project? Small projects should be fine. Big projects should evaluate carefully (available libraries, resources to fix a bug or two upstream, etc.).

"Does node run on 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.

"Can I access the DOM in node?"

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 implementing the DOM as a node module, which may open very exciting possibilities such as unit testing client-side code.

"Isn‘t event driven programming really hard?"

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.

Either way, test driven development can really help you to come up with maintainable designs.

"Who is using it?"

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 a mind-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.

"Where can I learn more?"

Tim Caswell is running the excellent How To Node blog. Follow #nodejs on twitter. Subscribe to the mailing list. And come and hang out in the IRC channel, #node.js (yes, the dot is in the name). We‘re close to hitting the 200 lurker-mark there soon : ).

I‘ll also continue to write articles here on debuggable.com.

That‘s it for now. Feel free to comment if you have more questions!

--fg

*: The metaphor is obviously a simplification, but if it‘s hard to find a counterpart for the concept of non-blocking in reality.

时间: 2024-10-12 23:19:34

Understanding node.js by Felix Geisendörfer(翻译 by shangyan)的相关文章

Understanding node.js

Understanding node.js Posted on 29/4/10 by Felix Geisendörfer Node.js has generally caused two reactions in people I've introduced it to. Basically people either "got it" right away, or they ended up being very confused. If you have been in the

[转载]Node入门 » 一本全面的Node.js教程

http://www.nodebeginner.org/index-zh-cn.html 作者: Manuel Kiessling 翻译: goddyzhao & GrayZhang & MondayChen 关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版.因此,只有当进行错误更正以及针对新版本Node.js的改动进行对应的修正时,才会进行

Node.js入门教程

http://www.nodebeginner.org/index-zh-cn.html#a-basic-http-server Node入门 作者: Manuel Kiessling翻译: goddyzhao & GrayZhang & MondayChen 关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版.因此,只有当进行错误更正以及针对

创业笔记-Node.js入门之基于事件驱动的回调

基于事件驱动的回调 这个问题可不好回答(至少对我来说),不过这是Node.js原生的工作方式.它是事件驱动的,这也是它为什么这么快的原因. 你也许会想花点时间读一下Felix Geisendörfer的大作Understanding node.js,它介绍了一些背景知识. 这一切都归结于“Node.js是事件驱动的”这一事实.好吧,其实我也不是特别确切的了解这句话的意思.不过我会试着解释,为什么它对我们用Node.js写网络应用(Web based application)是有意义的. 当我们使

Node.js入门学习笔记(三)

基于事件驱动的回调 这个问题不好回答,不过这是Node.js原生的工作方式.它是事件驱动的,这也是它为什么这么快的原因.你可以花一点时间阅读一下Felix Geisendörfer的大作 Understanding node.js 可了解一些背景知识. 当我们使用http.createServer方法的时候,我们当然不只是想要一个侦听某个端口的服务器,我们还想要这在服务器收到一个HTTP请求的时候做点什么. 问题是,这是异步的,请求任何时候都可能到达,但是我们的服务器却跑在一个单进程中. 写PH

node.js精彩入门手册

最近刚好在学习nodejs,发现了一份不错的文档,分享一下 JavaScript与Node.js JavaScript与你 抛开技术,我们先来聊聊你以及你和JavaScript的关系.本章的主要目的是想让你看看,对你而言是否有必要继续阅读后续章节的内容. 如果你和我一样,那么你很早就开始利用HTML进行“开发”,正因如此,你接触到了这个叫JavaScript有趣的东西,而对于JavaScript,你只会基本的操作——为web页面添加交互. 而你真正想要的是“干货”,你想要知道如何构建复杂的web

Node.js 使用process.nextTick

今天发现Node.js文档很好地解释了如何使用process.nextTick. Node.js文档链接 http://nodejs.org/api/process.html#process_process_nexttick_callback process.nextTick(function callback(){ }); Node.js确保callback会在处理下一个事件前被调用. 下面是Node.js文档的翻译: process.nextTick(callback) 在下一个事件循环中调

node.js 的事件驱动

照抄自Understanding node.js Node is basically very good when you need to do several things at the same time. Have you ever written a piece of code and said "I wish this would run in parallel"? Well, in node everything runs in parallel, except your

Node.js中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具

做为一个程序员可能在学习技术,了解行业新动态,解决问题时经常需要阅读英文的内容:而像我这样的英文小白就只能借助翻译工具才能理解个大概:不禁经常感慨,英文对学习计算机相关知识太重要了!最近发现IBM的云平台Blumemix,并且提供语言翻译的服务,感觉不错,就拿来研究学习一下:这里就分享一下我的研究学习过程,如何使用Node.js调用REST API打造自己的在线翻译工具,并演示如何把它发布到云平台上,让每个人都可以通过网络访问使用它. 应用效果展示 您可以通过点击效果图片的链接访问它. 构建一个