Understanding node.js

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

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:

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.

"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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/497841.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

腾讯AI Lab刷新人脸检测与识别两大测评国际记录,技术日调用超六亿

来源:腾讯AI实验室概要:人脸检测是让机器找到图像视频中所有人脸并精准定位其位置信息,人脸识别是基于人脸图像自动辨识其身份,两者密切相关,前者是后者的前提和基础。腾讯AI Lab在国际最大、最难的人脸检测平台WIDER …

修改数据库参数oracle,Oracle 修改数据库基本参数

Oracle 通过ALTER SYSTEM语句修改数据库参数,其语法:ALTER SYSTEM SET scope[ memory | spfile | both ] [sid]SCOPE表示应用的范围,分成三种memeory:只在当前实例中修改生效,重启后失效(内存中)spfile:只…

Jeff Dean| 面向系统的机器学习和面向机器学习的系统

来源:全球人工智能概要:我们将发布一系列内容,展示 Google 在 NIPS 2017 上发布的工作成果。首当其冲的便是来自 Jeff Dean 的 PPT 《面向系统的机器学习和面向机器学习的系统》。第 31 届神经信息处理系统年会(NIPS 2017&#xf…

mysql linux附加数据库文件夹,Linux全攻略--MySQL数据库配置与管理

MySQL是一种精巧的,多用户和多线程的中小型SQL数据库系统,由一个服务器守护进程mysqld和很多不同的客户程序和库组成.现在形成了一个"LinuxApachePHPMySQL"构建电子商务网站的黄金网站的黄金组合.MySQL不是开放源代码的产品,但在某些情况下可以自由使用.由于它的强大…

互联网如何促进经济发展?腾讯、谷歌、Facebook的实践和经验

来源:腾讯研究院右起:司晓 腾讯研究院院长李刚 腾讯研究院首席研究员Nicole Sremlau 牛津大学比较媒体法与政策项目负责人Pankaj Venugopal Facebook公司副总法律顾问Olivia Hatalsky Alphabet公司高级项目经理互联网正在成为社会生态的连接器&…

Execution in the Kingdom of Nouns (名词王国中的死刑)

来源:http://www.cnblogs.com/bigfish--/archive/2011/12/31/2308407.html Steve Yegge的大作 Execution in the Kingdom of Nounshttp://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html THURSDAY, MARCH 30, 2006 Execution in the Kingd…

linux 串口text模式安装,使用串口安装centos操作系统

使用CSS3 Media Queries实现网页自适应原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...Ajax读取txt并对txt内容进行分页显示function TransferString(content) { var string content…

汇真科技李利鹏 :人工智能的应用边界

人工智能分为几个层面,首先是基础层,要有大数据云计算,因为你数据量大的话,要放到云端去处理,大数据、云计算、GPU/FPGA等硬件加速、新形态神经网络芯片等计算能力提供商。在技术层就是做机器学习、深度学习、增强学习…

linux java内存清理,Linux服务器用完java内存

寻找解决方法或关于如何找出问题的一些提示。Linux服务器用完java内存使用只显示引用的VisualVM工具查看heapdumps正在举行。有更好的工具可以使用吗?有什么我可以从命令行运行以释放这些引用?使用jconsole GC不起作用,只能延长约5天的锁定时…

中缀到后缀的转换

中缀表达式:ab*c(d*ef)*g 转换成后缀表达式:abc*de*fg* 转换步骤: 当读到一个操作数(如:a)时,立即把它放到输出中; 当遇到操作符(如:)时&#…

CES 2018即将揭幕:AI平台之争烽烟骤起

来源:机器人创新生态概要:一年一度的国际消费电子展(CES)即将揭开序幕,今年有哪些值得观察的热门技术呢?一年一度的国际消费电子展(CES)即将揭开序幕,今年有哪些值得观察…

后缀表达式转变成表达式树

后缀表达式:abcde** 转换的表达式树: 转换步骤: 如果符号是操作数,那么就建立一个单节点树并将它推入栈中。 如果符号是操作符,那么就从栈中弹出两颗树 T1 和 T2 (T1 先弹出)并形成一棵新的树…

linux用户组登录,linux用户和用户组

1.用户组和权限管理用户分为三类:1.1 超级用户:root uid0,简单来说uid为0的用户就是超级用户。1.2 虚拟用户:存在linux中,满足文件或者程序运行的需要,而创建的。不能登录,不能使用。uid1-499,1-999(centos…

人工智能和厨房电器的结合:厨房助理机器人

来源:人工智能学家AItists概要:从智能家居到无人驾驶,人工智能正在落地的道路上。而其中一个让人觉得有点意外也很有趣的场景是:厨房。从智能家居到无人驾驶,人工智能正在落地的道路上。而其中一个让人觉得有点意外也很…

查找N个数中第K大的数

方法一:将一组数放入数组中,升排序。并返回第 ( length - k )个元素这里消耗的时间就是排序用的时间,用快速排序则为:O( N log N )代码: /*** 先排序后获取* return** Date :2012-7-4* Author :GongQiang*/public in…

美国科学院学报:如何在竞争激烈的环境下维持稳定的群体

行为决策演化示意图。最上面一行是混合群体,下面两行是网络群体。相比于混合群体,网络促使合作者聚集成团簇,但是惩罚的加入将破坏合作环境。来源:中国科学院西安光机所李学龙研究员同合作者在数据驱动的行为决策研究方面取得阶段…

linux文本处理脚本题,Linux文本处理工具sed练习题

1、使用sed命令打印出ifconfig ens33的ip地址解:(1)ifconfig ens33 | sed -n 2p | sed s/.*inet // | sed s/netmask.*//(2)ifconfig ens33 | sed -n 2p | sed -r s/.*inet (.*)netmask.*/\1/(3)ifconfig ens33 | sed -r -n 2s/.*inet (.*)netmask.*/\1/p2、使用sed命令打印出系…

全球首个Magic Leap One体验:吓到你不敢进房间

来源:智东西概要:业内备受关注的AR技术公司Magic Leap,在获得19亿美元融资历经七年之后,终于放出其第一款头盔产品Magic Leap One,很快在科技圈、VR圈引起刷屏式关注。昨夜,业内备受关注的AR技术公司Magic …

ftp+linux+使用webdav,群晖-win/mac/nfs ftp tftp webdav文件服务的概念及设置

应用程序级别WebDAV 是一个 HTTP 的扩充服务,可让用户编辑和管理存储在远程服务器上的文件。通过 Synology DiskStation Manager 的 WebDAV 服务,支持 WebDAV 的客户端程序(如 Windows 资源管理器、Mac OS Finder、Linux 资源管理器)将能够远程访问 Syno…

二叉堆

满足如下结构性和堆序性,即为二叉堆。 结构性质:堆是一棵被完全填满的二叉树,有可能的例外是在底层,底层上的元素从左到右填入。这样的树称为完全二叉树。 容易证明,一棵高为 h 的完全二叉树有 2h 到 2h1-1 个节点。这…