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 …

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公司高级项目经理互联网正在成为社会生态的连接器&…

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

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

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概要:从智能家居到无人驾驶,人工智能正在落地的道路上。而其中一个让人觉得有点意外也很有趣的场景是:厨房。从智能家居到无人驾驶,人工智能正在落地的道路上。而其中一个让人觉得有点意外也很…

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

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

全球首个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 个节点。这…

伯克利AI研究院解析「反向课程学习」,改善「强化学习智能体」并应用于机器人技术

原文来源:arXiv作者:Wieland Brendel、Jonas Rauber、Matthias Bethge「雷克世界」编译:嗯~阿童木呀、哆啦A亮众所周知,强化学习(RL)是一种强大的技术,它能够解决诸如移动(locomotio…

《Science》评选2017年十大科学突破,看看有哪些吧!

来源:材料科学与工程概要:美国《科学》杂志21日公布了其评选出的2017年十大科学突破,人类首次观测到双中子星并合事件被选为头号科学突破。美国《科学》杂志21日公布了其评选出的2017年十大科学突破,人类首次观测到双中子星并合事…

2017年深度学习重大研究进展全解读

来源:机器之心概要:想知道哪些深度学习技术即将影响我们的未来吗?本文将给你作出解答。2017 年只剩不到十天,随着 NIPS 等重要会议的结束,是时候对这一年深度学习领域的重要研究与进展进行总结了。来自机器学习创业公司…

linux管道符加空格吗,管道符,作业控制,变量以及变量配置文件

一、管道符前面已经提过过管道符 “|”, 就是把前面的命令运行的结果丢给后面的命令。# cat 1.txt | wc -l //显示1.txt文件行数二、作业控制当运行进程时,你可以使它暂停(按CtrlZ组合键),然后使用fg(foreground的简写)命令恢复它,或是利用bg…

自动驾驶汽车硬件与软件技术介绍

来源:知乎概要:本文详细介绍了自动驾驶汽车的硬件和软件,以及所需要做的准备工作,每个研发者或者准备投身于无人驾驶领域的人都应该好好看一下。全球有数不清的公司在忙着研发自动驾驶汽车,他们的产品也千奇百怪&#…

DeepMind 的2017:有 AlphaGo,更有社会责任

来源:AI科技评论概要:DeepMind 相信 AI 在更复杂的问题上也能起到同样的作用,它可以是科学技术工具,也可以是人类创造力的倍增器。AlphaGo 团队现在已经把注意力转向了另一组宏伟的目标,DeepMind 自己也希望这些研究人…

2018年人工智能13大预测

来源:英伟达概要:2017 年人工智能领域取得了许多里程碑式的成果。那么,以后人工智能又会如何发展呢?2017年被《华尔街日报》、《福布斯》和《财富》等刊物称为“人工智能之年”。各种深度学习在线课程不断推出,接受相关…