Node.js 中的节点到底是什么?

发布于 2024-09-11 03:21:38 字数 614 浏览 6 评论 0原文

在 Erlang 中,我能够立即理解“节点”的概念——一个独立的 Erlang 虚拟机。我可以使用 erl -name gandalf -setcookie abc 在一台计算机上启动一个节点,并使用 erl -name bilbo -setcookie abc 在另一台计算机(位于同一 LAN 上)上启动另一个节点。 >。然后我可以在 gandalf 上生成进程,这些进程将与 bilbo 上的其他进程进行神奇的通信。现在,由于我还想提供一个带有来自 Erlang 进程的动画图形结果的爵士网页,所以我学习了一些 Javascript 并学习了 jQuery。仍然是一个谦虚的 paduwan,但我有点理解 Javascript 如何适应事物的计划。

我最近遇到了 Node.js,一个(邪恶的)声音开始低声说道:“就是它了!”现在您可以使用 Javascript 完成所有操作!忘记 Erlang、守卫和句点,坚持使用每个人都使用的语言。”

我已经阅读了一些文档,但我仍然不明白 Node.js 中的节点是什么。我是否必须运行一个 http 服务器才能成为我的节点?如果我不喜欢 http,或者我不关心 gandalf 如何与 bilbo 对话怎么办 - 这就是我在 Erlang 中喜欢的。也许我真的很希望node.js 是带有Javascript 糖的erlang?

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.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唔猫 2024-09-18 03:21:38

Node.js 与 Twisted 的共同点比 Erlang/OTP 更多。 Node.js 只是一个单线程 SEDA 事件循环。在分发、热代码重载和通过进程的可扩展性方面,Node.js 无法与 Erlang VM 相比,它与“带有 Javascript 糖的 Erlang”根本不相上下。

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"

鸵鸟症 2024-09-18 03:21:38

也许由于您的 Erlang 知识,您认为 Node.js 在某种程度上与“节点”(作为 erlang 节点)有关,但这只是名称。

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

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

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

在 Node 中,您将以一种非常不同的方式进行编码:

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

如果 Node.js 脚本中的代码错误,例如:

while(true) { }

那么您将阻塞该进程,并且它将无法处理比当前请求更多的请求,因此在 Node.js 中必须遵循上述准则。

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.

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.

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:

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:

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.

等风来 2024-09-18 03:21:38

据我了解,Node.JS 节点是 V8 引擎的一个实例,其中运行 Node.JS 运行时和事件循环。虽然 Node.JS 运行时使您能够非常快速、简单地开始处理 HTTP 请求,但这不是强制性的;它确实非常擅长处理大多数类型的异步 I/O。

我对Erlang了解不多,但我粗浅的了解是它的强大之处在于高并发计算。 Node.JS 本身并不专注于此。它的核心是“事件化I/O”,干净利落地处理异步I/O。

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.

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.

请别遗忘我 2024-09-18 03:21:38

正如前面提到的, node.js 中没有“节点”

,当您运行时,

node my_script.js 

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

there is no "node" in node.js

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文