热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

node.js中的节点到底是什么?-Whatexactlyisanodeinnode.js?

InErlangIwasabletoimmediatelyunderstandthenotionofanode-aself-containedErlangVM.

In Erlang I was able to immediately understand the notion of a 'node' - a self-contained Erlang VM. I could start a node on one machine with erl -name gandalf -setCOOKIE abc, and another node on another machine (on the same LAN) with erl -name bilbo -setCOOKIE abc. I could then spawn processes on gandalf which would communicate magically with other processes on bilbo. Now, since I also wanted to serve up a jazzy webpage with animated graphical results from my Erlang processes, I picked up some Javascript and learnt jQuery. Still a humble paduwan, but I sort of understand how Javascript fits into the scheme of things.

在Erlang中,我能够立即理解“节点”的概念——一个自包含的Erlang VM。我可以在一台机器上用erl -name gandalf -setCOOKIE abc启动一个节点,在另一台机器上(在同一个局域网上)用erl -name bilbo -setCOOKIE abc启动另一个节点。然后,我可以在gandalf上生成进程,它将与bilbo上的其他进程通信。现在,由于我也想用Erlang过程生成的动画图形结果创建一个花哨的网页,所以我学习了一些Javascript并学习了jQuery。仍然是一个不起眼的paduwan,但是我有点理解Javascript是如何融入事物的计划的。

I recently came across node.js and an (evil) voice started whispering: 'This is it! Now you can do everything with Javascript! Forget Erlang and guards and periods, stick to a language that everyone uses'.

我最近遇到node。js和一个(邪恶的)声音开始窃窃私语:“就是这个!”现在你可以用Javascript做任何事情了!忘掉Erlang、警卫和句号,坚持大家都用的语言。

I've read the docs a bit, but I still don't understand what a node is in node.js. Do I have to run a http server and that becomes my node? What if I don't like http, or I don't care how gandalf talks to bilbo - that's what I like in Erlang. Maybe I nai:vely expect that node.js is erlang with Javascript sugar?

我已经读了一些文档,但是我仍然不理解node.js中的节点是什么。我是否必须运行一个http服务器,然后它就成为我的节点?如果我不喜欢http,或者我不在乎甘道夫怎么和比尔博说话——这就是我喜欢Erlang的地方。也许我奈:非常期待那个节点。js是erlang和Javascript糖吗?

4 个解决方案

#1


7  

Node.js has much more in common with Twisted than Erlang/OTP. Node.js is just a single threaded SEDA event loop. Node.js has nothing compared to Erlang VM when it comes to distribution, hot code reloading, and scalability via processes, it isn't anything close to "Erlang with Javascript sugar"

节点。js与Twisted比Erlang/OTP有更多的共同之处。节点。js只是一个单线程SEDA事件循环。节点。在分发、热代码重载和进程可扩展性等方面,js与Erlang虚拟机相比简直是小巫见大巫

#2


7  

Maybe because of your Erlang knowledge you thought that somehow Node.js had something to do with "nodes" (as erlang nodes), but it's just the name.

也许是因为你的Erlang知识,你认为这个节点。js与“节点”(如erlang节点)有关,但它只是名称。

The main idea with Node.js is that you defer all expensive I/O operations and assign callbacks to the result of those operations. The reason is that I/O blocks the (only) process that is running at the moment. Node.js will handle this for you, given that you are coding in the proper way.

节点的主要思想。js是延迟所有昂贵的I/O操作并为这些操作的结果分配回调。原因是I/O阻塞了当前正在运行的(惟一的)进程。节点。js将为您处理这个问题,因为您正在以正确的方式进行编码。

An easy example of this is a database call:

一个简单的例子是数据库调用:

result = SQL.query("EXPENSIVE SELECT HERE")
doSomething(result);
moreStuff(); // This line must wait until the previous ones are completed.

In node you would code this in a very different way:

在node,你可以用一种非常不同的方式来编码:

SQL.query("EXPENSIVE SELECT HERE", function(result) {
  doSomething(result);
});
moreStuff(); // This line executes inmediately

If you have wrong code in your Node.js script, like:

如果您的节点中有错误的代码。js脚本,如:

while(true) { }

Then you are blocking the process and it won't be able to handle more requests than the current one, so in Node.js is mandatory to follow the above guidelines.

然后阻塞进程,它将无法处理比当前请求更多的请求,因此在Node中。js必须遵守以上准则。

#3


4  

As I understand it, a Node.JS node is an instance of the V8 engine with the Node.JS runtime and event-loop running in it. While the Node.JS runtime gives you the ability to very quickly and simply begin processing HTTP requests, it's not mandatory; it is very good at handling most any kind of asynchronous I/O, really.

根据我的理解,是一个节点。JS节点是带有该节点的V8引擎的实例。JS运行时和事件循环在其中运行。而节点。JS运行时使您能够非常快速地开始处理HTTP请求,这不是强制性的;它非常擅长处理任何类型的异步I/O。

I don't know that much about Erlang, but my superficial understanding is that its great strength is high-concurrency computing. Node.JS doesn't specialize in that, per se. Its heart is "evented I/O", dealing neatly and cleanly with asynchronous I/O.

我不太了解Erlang,但我的表面理解是它的强大之处在于高并发计算。节点。JS本身并不擅长这个。它的核心是“唤醒I/O”,处理异步I/O时干净利落。

#4


1  

there is no "node" in node.js

node.js中没有“node”

as mentioned, when you run

如前所述,当您运行时

node my_script.js 

you are running one instance of V8 java script interpreter (which is using one core for its lifetime).

您正在运行一个V8 java脚本解释器实例(在其生命周期中使用一个核心)。


推荐阅读
author-avatar
永欣慧娟766
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有