我不完全明白 Node.js 的全部内容。也许是因为我主要是一个基于网络的业务应用程序开发人员。它是什么以及它有什么用?
到目前为止我的理解是:
- 编程模型是事件驱动的,尤其是它处理 I/O 的方式。
- 它使用 JavaScript ,解析器是 V8。
- 它可以轻松地用于创建并发服务器应用程序。
我的理解正确吗?如果是,那么事件 I/O 的好处是什么?它只是更适合并发性吗?另外,Node.js 的方向是成为一个类似基于 JavaScript(基于 V8)的编程模型的框架吗?
I don't fully get what Node.js is all about. Maybe it's because I am mainly a web based business application developer. What is it and what is the use of it?
My understanding so far is that:
- The programming model is event driven, especially the way it handles I/O.
- It uses JavaScript and the parser is V8.
- It can be easily used to create concurrent server applications.
Are my understandings correct? If yes, then what are the benefits of evented I/O, is it just more for the concurrency stuff? Also, is the direction of Node.js to become a framework like, JavaScript based (V8 based) programming model?
发布评论
评论(10)
另外,不要忘记提及 Google 的 V8 非常快。它实际上将 JavaScript 代码转换为机器代码,并具有与编译后的二进制文件相匹配的性能。因此,与所有其他伟大的事情一样,它的速度非常快。
Also, do not forget to mention that Google's V8 is VERY fast. It actually converts the JavaScript code to machine code with the matched performance of compiled binary. So along with all the other great things, it's INSANELY fast.
问:编程模型是事件驱动的,尤其是它处理 I/O 的方式。
正确的。它使用回调,因此任何访问文件系统的请求都会导致将请求发送到文件系统,然后 Node.js 将开始处理其下一个请求。它只会在从文件系统获得响应后才担心 I/O 请求,此时它将运行回调代码。但是,可以发出同步 I/O 请求(即阻塞请求)。由开发人员选择异步(回调)或同步(等待)。
问:它使用 JavaScript,解析器是 V8。
是的
问:它可以很容易地用于创建并发服务器应用程序。
是的,尽管您需要手动编写大量 JavaScript 代码。最好查看一个框架,例如 http://www.easynodejs.com/ - 其中附带完整的在线文档和示例应用程序。
Q: The programming model is event driven, especially the way it handles I/O.
Correct. It uses call-backs, so any request to access the file system would cause a request to be sent to the file system and then Node.js would start processing its next request. It would only worry about the I/O request once it gets a response back from the file system, at which time it will run the callback code. However, it is possible to make synchronous I/O requests (that is, blocking requests). It is up to the developer to choose between asynchronous (callbacks) or synchronous (waiting).
Q: It uses JavaScript and the parser is V8.
Yes
Q: It can be easily used to create concurrent server applications.
Yes, although you'd need to hand-code quite a lot of JavaScript. It might be better to look at a framework, such as http://www.easynodejs.com/ - which comes with full online documentation and a sample application.
我在工作中使用 Node.js,发现它非常强大。被迫选择一个词来描述 Node.js,我会说“有趣”(这不是一个纯粹的积极形容词)。社区充满活力并不断发展。尽管 JavaScript 很奇怪,但它仍然是一种很棒的编码语言。而且您每天都会重新思考自己对“最佳实践”和结构良好的代码模式的理解。现在有大量的想法涌入 Node.js,在其中工作会让你接触到所有这些想法 - 伟大的精神举重。
Node.js 在生产中肯定是可能的,但距离文档中看似承诺的“交钥匙”部署还很远。在 Node.js v0.6.x 中,“集群”已集成到平台中,提供了基本的构建块之一,但我的“product.js”脚本仍然有约 150 行逻辑来处理创建日志等内容对于“严肃的”生产服务,您还需要准备好限制传入连接并执行 Apache 为 PHP。公平地说,Ruby on Rails 确实存在这个问题。它可以通过两种互补的机制来解决:1)将 Ruby on Rails/Node.js 放在专用的 Web 服务器后面(用 C 编写并经过反复测试),例如 Nginx (或 Apache / < a href="http://en.wikipedia.org/wiki/Lighttpd" rel="nofollow noreferrer">Lighttd)。网络服务器可以有效地提供静态内容、访问日志记录、重写 URL、终止 SSL、强制执行访问规则,并管理多个子服务。对于命中实际节点服务的请求,Web 服务器会代理该请求。 2)使用像 Unicorn 这样的框架来管理工作进程,定期回收它们等。我已经尚未找到一个看起来完全成熟的 Node.js 服务框架;它可能存在,但我还没有找到它,并且仍在我的手卷“生产.js”中使用〜150行。
阅读像 Express 这样的框架,看起来标准做法就是通过一个万事通来提供一切服务Node.js 服务...“app.use(express.static(__dirname + '/public'))”。对于低负载服务和开发来说,这可能没问题。但是,一旦您尝试在服务上投入大量时间并让它 24/7 运行,您很快就会发现推动大型网站拥有良好烘焙、强化的 C 代码(如 Nginx 负责其网站并处理所有静态内容请求(...直到您设置 CDN,例如 Amazon CloudFront))。对于对此的幽默和毫不掩饰的负面看法,请参阅这个人。
Node.js 也发现了越来越多的非服务用途。即使您使用其他东西来提供 Web 内容,您仍然可以使用 Node.js 作为构建工具,使用 npm 模块来组织你的代码,Browserify 来缝合它到单个资产中,并 uglify-js 将其缩小以进行部署。对于处理网络来说,JavaScript 是一个完美的阻抗匹配,并且经常使它成为最简单的攻击路线。例如,如果您想通过一堆 JSON 响应负载,您应该使用我的underscore-CLI 模块,结构化数据的实用带。
优点/缺点:
与简单的每个请求一个进程模型 (LAMP) 相比:
与用 Java / C# / C(C?真的吗?)编写“真正的”服务相比,
有关 JavaScript 和 Node.js 的另一个视角,请查看 从 Java 到 Node.js,一篇关于 Java 开发者学习 Node.js 的印象和经验的博文。
模块
在考虑 Node 时,请记住,您选择的 JavaScript 库将定义您的体验。大多数人至少使用两个,一个异步模式帮助器(Step、Futures、Async)和一个 JavaScript 糖模块(Underscore.js)。
Helper / JavaScript Sugar:
异步模式模块:
或者要阅读有关异步库的所有信息,请参阅此小组访谈 与作者一起。
Web 框架:
测试:
另外,请查看推荐的 Node.js 模块的官方列表。但是, GitHub 的 Node Modules Wiki 更加完整,是一个很好的资源。
要理解 Node,考虑一些关键的设计选择会很有帮助:
Node.js 是基于事件和异步 / 非的-阻止。事件(例如传入的 HTTP 连接)将触发 JavaScript 函数,该函数执行一些工作并启动其他异步任务,例如连接到数据库或从另一台服务器提取内容。一旦这些任务开始,事件函数就会完成,Node.js 将返回睡眠状态。一旦发生其他事情,例如建立数据库连接或外部服务器响应内容,回调函数就会触发,并且执行更多 JavaScript 代码,可能会启动更多异步任务(例如数据库查询)。通过这种方式,Node.js 将愉快地为多个并行工作流交错活动,运行任何在任何时间点都畅通无阻的活动。这就是 Node.js 在管理数千个并发连接方面做得如此出色的原因。
为什么不像其他人那样为每个连接使用一个进程/线程?在 Node.js 中,一个新连接只是一个非常小的堆分配。启动一个新进程需要更多的内存,在某些平台上需要兆字节。但真正的成本是与上下文切换相关的开销。当您有 10^6 个内核线程时,内核必须做大量工作来确定下一个应该执行的人。为 Linux 构建 O(1) 调度程序已经做了很多工作,但最终,单个事件驱动进程比竞争 CPU 时间的 10^6 个进程要高效得多。此外,在过载情况下,多进程模型的表现非常糟糕,导致关键的管理和管理服务匮乏,尤其是 SSHD(这意味着您甚至无法登录到盒子来弄清楚它到底有多糟糕)。
Node.js 是单线程并且无锁。 Node.js 作为一个经过深思熟虑的设计选择,每个进程只有一个线程。因此,多个线程根本不可能同时访问数据。因此,不需要锁。线程很难。真的真的很难。如果您不相信这一点,那么您还没有进行足够的线程编程。正确锁定是很困难的,并且会导致很难追踪的错误。消除锁和多线程可以让最令人讨厌的错误之一消失。这可能是节点的最大优势。
但是我如何利用我的 16 核盒子?有
两种方法:
Node.js 让您可以毫不费力地完成一些非常强大的事情。假设您有一个 Node.js 程序,可以执行各种任务,监听 TCP 命令端口,对一些图像进行编码,等等。通过五行代码,您可以添加基于 HTTP 的 Web 管理门户,该门户显示活动任务的当前状态。这很容易做到:
现在您可以点击 URL 并检查正在运行的进程的状态。添加几个按钮,您就拥有了一个“管理门户”。如果您有一个正在运行的 Perl / Python / Ruby 脚本,那么仅仅“投入管理门户”并不那么简单。
但是 JavaScript 不是很慢/坏/邪恶/恶魔的产生吗?JavaScript 有一些奇怪的地方,但是有了“好的部分”,就有了一个非常强大的功能那里的语言,无论如何,JavaScript 是客户端(浏览器)上的语言。 JavaScript 将会继续存在;其他语言将其作为 IL 的目标,世界一流的人才正在竞相开发最先进的 JavaScript 引擎。由于 JavaScript 在浏览器中的作用,人们投入了大量的工程工作来使 JavaScript 变得极快。 V8 是最新、最好的 JavaScript 引擎,至少本月是这样。它在效率和稳定性方面都击败了其他脚本语言(看看你,Ruby)。只有微软、谷歌和 Mozilla 的庞大团队致力于解决这个问题,竞相构建最好的 JavaScript 引擎(它不再是 JavaScript“解释器”,因为所有现代引擎都会做大量的JIT 在幕后编译,解释仅作为执行一次代码的后备)。是的,我们都希望能够修复一些更奇怪的 JavaScript 语言选择,但这实际上并没有那么糟糕。而且该语言非常灵活,您实际上不是在编写 JavaScript,而是在编写 Step 或 jQuery - 与任何其他语言相比,在 JavaScript 中,库更能定义体验。要构建 Web 应用程序,您几乎必须了解 JavaScript,因此在服务器上使用它进行编码具有某种技能组合的协同作用。它让我不再害怕编写客户端代码。
此外,如果你真的讨厌 JavaScript,你可以使用语法糖,如 CoffeeScript。或者任何其他创建 JavaScript 代码的工具,例如 Google Web Toolkit (GWT)。
说到 JavaScript,什么是“闭包”? - 这是一种在调用链中保留词法作用域变量的奇特方式。 ;) 像这样:
看看如何只使用“myData”而不做任何尴尬的事情,比如将其存储到对象中?与 Java 不同的是,“myData”变量不必是只读的。这种强大的语言功能使异步编程变得不再那么冗长和痛苦。
编写异步代码总是比编写简单的单线程脚本更复杂,但是使用 Node.js,这并没有那么困难,除了对数千个并发连接的效率和可扩展性之外,您还可以获得很多好处。 ..
I use Node.js at work, and find it to be very powerful. Forced to choose one word to describe Node.js, I'd say "interesting" (which is not a purely positive adjective). The community is vibrant and growing. JavaScript, despite its oddities can be a great language to code in. And you will daily rethink your own understanding of "best practice" and the patterns of well-structured code. There's an enormous energy of ideas flowing into Node.js right now, and working in it exposes you to all this thinking - great mental weightlifting.
Node.js in production is definitely possible, but far from the "turn-key" deployment seemingly promised by the documentation. With Node.js v0.6.x, "cluster" has been integrated into the platform, providing one of the essential building blocks, but my "production.js" script is still ~150 lines of logic to handle stuff like creating the log directory, recycling dead workers, etc. For a "serious" production service, you also need to be prepared to throttle incoming connections and do all the stuff that Apache does for PHP. To be fair, Ruby on Rails has this exact problem. It is solved via two complementary mechanisms: 1) Putting Ruby on Rails/Node.js behind a dedicated webserver (written in C and tested to hell and back) like Nginx (or Apache / Lighttd). The webserver can efficiently serve static content, access logging, rewrite URLs, terminate SSL, enforce access rules, and manage multiple sub-services. For requests that hit the actual node service, the webserver proxies the request through. 2) Using a framework like Unicorn that will manage the worker processes, recycle them periodically, etc. I've yet to find a Node.js serving framework that seems fully baked; it may exist, but I haven't found it yet and still use ~150 lines in my hand-rolled "production.js".
Reading frameworks like Express makes it seem like the standard practice is to just serve everything through one jack-of-all-trades Node.js service ... "app.use(express.static(__dirname + '/public'))". For lower-load services and development, that's probably fine. But as soon as you try to put big time load on your service and have it run 24/7, you'll quickly discover the motivations that push big sites to have well baked, hardened C-code like Nginx fronting their site and handling all of the static content requests (...until you set up a CDN, like Amazon CloudFront)). For a somewhat humorous and unabashedly negative take on this, see this guy.
Node.js is also finding more and more non-service uses. Even if you are using something else to serve web content, you might still use Node.js as a build tool, using npm modules to organize your code, Browserify to stitch it into a single asset, and uglify-js to minify it for deployment. For dealing with the web, JavaScript is a perfect impedance match and frequently that makes it the easiest route of attack. For example, if you want to grovel through a bunch of JSON response payloads, you should use my underscore-CLI module, the utility-belt of structured data.
Pros / Cons:
Versus a simple one-process-per-request model (LAMP):
Versus writing a "real" service in Java / C# / C (C? really?)
For another perspective on JavaScript and Node.js, check out From Java to Node.js, a blog post on a Java developer's impressions and experiences learning Node.js.
Modules
When considering node, keep in mind that your choice of JavaScript libraries will DEFINE your experience. Most people use at least two, an asynchronous pattern helper (Step, Futures, Async), and a JavaScript sugar module (Underscore.js).
Helper / JavaScript Sugar:
Asynchronous Pattern Modules:
Or to read all about the asynchronous libraries, see this panel-interview with the authors.
Web Framework:
Testing:
Also, check out the official list of recommended Node.js modules. However, GitHub's Node Modules Wiki is much more complete and a good resource.
To understand Node, it's helpful to consider a few of the key design choices:
Node.js is EVENT BASED and ASYNCHRONOUS / NON-BLOCKING. Events, like an incoming HTTP connection will fire off a JavaScript function that does a little bit of work and kicks off other asynchronous tasks like connecting to a database or pulling content from another server. Once these tasks have been kicked off, the event function finishes and Node.js goes back to sleep. As soon as something else happens, like the database connection being established or the external server responding with content, the callback functions fire, and more JavaScript code executes, potentially kicking off even more asynchronous tasks (like a database query). In this way, Node.js will happily interleave activities for multiple parallel workflows, running whatever activities are unblocked at any point in time. This is why Node.js does such a great job managing thousands of simultaneous connections.
Why not just use one process/thread per connection like everyone else? In Node.js, a new connection is just a very small heap allocation. Spinning up a new process takes significantly more memory, a megabyte on some platforms. But the real cost is the overhead associated with context-switching. When you have 10^6 kernel threads, the kernel has to do a lot of work figuring out who should execute next. A bunch of work has gone into building an O(1) scheduler for Linux, but in the end, it's just way way more efficient to have a single event-driven process than 10^6 processes competing for CPU time. Also, under overload conditions, the multi-process model behaves very poorly, starving critical administration and management services, especially SSHD (meaning you can't even log into the box to figure out how screwed it really is).
Node.js is SINGLE THREADED and LOCK FREE. Node.js, as a very deliberate design choice only has a single thread per process. Because of this, it's fundamentally impossible for multiple threads to access data simultaneously. Thus, no locks are needed. Threads are hard. Really really hard. If you don't believe that, you haven't done enough threaded programming. Getting locking right is hard and results in bugs that are really hard to track down. Eliminating locks and multi-threading makes one of the nastiest classes of bugs just go away. This might be the single biggest advantage of node.
But how do I take advantage of my 16 core box?
Two ways:
Node.js lets you do some really powerful things without breaking a sweat. Suppose you have a Node.js program that does a variety of tasks, listens on a TCP port for commands, encodes some images, whatever. With five lines of code, you can add in an HTTP based web management portal that shows the current status of active tasks. This is EASY to do:
Now you can hit a URL and check the status of your running process. Add a few buttons, and you have a "management portal". If you have a running Perl / Python / Ruby script, just "throwing in a management portal" isn't exactly simple.
But isn't JavaScript slow / bad / evil / spawn-of-the-devil? JavaScript has some weird oddities, but with "the good parts" there's a very powerful language there, and in any case, JavaScript is THE language on the client (browser). JavaScript is here to stay; other languages are targeting it as an IL, and world class talent is competing to produce the most advanced JavaScript engines. Because of JavaScript's role in the browser, an enormous amount of engineering effort is being thrown at making JavaScript blazing fast. V8 is the latest and greatest javascript engine, at least for this month. It blows away the other scripting languages in both efficiency AND stability (looking at you, Ruby). And it's only going to get better with huge teams working on the problem at Microsoft, Google, and Mozilla, competing to build the best JavaScript engine (It's no longer a JavaScript "interpreter" as all the modern engines do tons of JIT compiling under the hood with interpretation only as a fallback for execute-once code). Yeah, we all wish we could fix a few of the odder JavaScript language choices, but it's really not that bad. And the language is so darn flexible that you really aren't coding JavaScript, you are coding Step or jQuery -- more than any other language, in JavaScript, the libraries define the experience. To build web applications, you pretty much have to know JavaScript anyway, so coding with it on the server has a sort of skill-set synergy. It has made me not dread writing client code.
Besides, if you REALLY hate JavaScript, you can use syntactic sugar like CoffeeScript. Or anything else that creates JavaScript code, like Google Web Toolkit (GWT).
Speaking of JavaScript, what's a "closure"? - Pretty much a fancy way of saying that you retain lexically scoped variables across call chains. ;) Like this:
See how you can just use "myData" without doing anything awkward like stashing it into an object? And unlike in Java, the "myData" variable doesn't have to be read-only. This powerful language feature makes asynchronous-programming much less verbose and less painful.
Writing asynchronous code is always going to be more complex than writing a simple single-threaded script, but with Node.js, it's not that much harder and you get a lot of benefits in addition to the efficiency and scalability to thousands of concurrent connections...
我认为优点是:
在虚拟机 (V8) 上使用动态语言 (JavaScript) 进行 Web 开发,速度非常快。它比 Ruby、Python 或 Perl 快得多。
能够在单个进程上以最小的开销处理数千个并发连接。
能够
JavaScript 非常适合具有一流函数对象和闭包的事件循环。人们已经知道如何以这种方式使用它,并在浏览器中使用它来响应用户发起的事件。
很多人已经了解 JavaScript,即使是那些不自称是程序员的人。它可以说是最流行的编程语言。
在 Web 服务器和浏览器上使用 JavaScript 可以减少两个编程环境之间的阻抗不匹配,这两个编程环境可以通过 JSON 来通信数据结构,这些数据结构在等式两边的工作方式相同。重复的表单验证代码可以在服务器和客户端等之间共享。
I think the advantages are:
Web development in a dynamic language (JavaScript) on a VM that is incredibly fast (V8). It is much faster than Ruby, Python, or Perl.
Ability to handle thousands of concurrent connections with minimal overhead on a single process.
JavaScript is perfect for event loops with first class function objects and closures. People already know how to use it this way having used it in the browser to respond to user initiated events.
A lot of people already know JavaScript, even people who do not claim to be programmers. It is arguably the most popular programming language.
Using JavaScript on a web server as well as the browser reduces the impedance mismatch between the two programming environments which can communicate data structures via JSON that work the same on both sides of the equation. Duplicate form validation code can be shared between server and client, etc.
V8 是 JavaScript 的实现。它允许您运行独立的 JavaScript 应用程序(除其他外)。
Node.js 只是一个为 V8 编写的库,它执行事件 I/O。这个概念解释起来有点棘手,我相信有人会用比我更好的解释来回答......要点是,你不需要做一些输入或输出并等待它发生,你只需不做不用等待它完成。例如,询问文件的上次编辑时间:
这可能需要几毫秒,也可能需要几秒钟。通过事件 I/O,您只需触发请求,而不是等待附加请求完成时运行的回调:
这使得它很像浏览器中的 JavaScript 代码(例如,使用 Ajax 风格的功能)。
有关更多信息,您应该查看文章Node.js 确实令人兴奋 这是我对库/平台的介绍...我发现它非常好。
V8 is an implementation of JavaScript. It lets you run standalone JavaScript applications (among other things).
Node.js is simply a library written for V8 which does evented I/O. This concept is a bit trickier to explain, and I'm sure someone will answer with a better explanation than I... The gist is that rather than doing some input or output and waiting for it to happen, you just don't wait for it to finish. So for example, ask for the last edited time of a file:
That might take a couple of milliseconds, or it might take seconds. With evented I/O you simply fire off the request and instead of waiting around you attach a callback that gets run when the request finishes:
This makes it a lot like JavaScript code in the browser (for example, with Ajax style functionality).
For more information, you should check out the article Node.js is genuinely exciting which was my introduction to the library/platform... I found it quite good.
Node.js 是一个为服务器端 JavaScript 代码构建的开源命令行工具。您可以下载tarball,编译并安装源代码。它允许您运行 JavaScript 程序。
JavaScript 由 V8 执行,这是 Google 开发的 JavaScript 引擎,用于 Chrome 浏览器。它使用 JavaScript API 来访问网络和文件系统。
它因其性能和执行并行操作的能力而广受欢迎。
以下是一些关于该主题的好文章。
Node.js is an open source command line tool built for the server side JavaScript code. You can download a tarball, compile and install the source. It lets you run JavaScript programs.
The JavaScript is executed by the V8, a JavaScript engine developed by Google which is used in Chrome browser. It uses a JavaScript API to access the network and file system.
It is popular for its performance and the ability to perform parallel operations.
Following are some good articles on the topic.
闭包是在创建代码的上下文中执行代码的一种方式。
这对于并发意味着您可以定义变量,然后启动非阻塞 I/O 函数,并向其发送一个匿名函数以进行回调。
当任务完成时,回调函数将在变量的上下文中执行,这就是闭包。
闭包如此适合编写具有非阻塞 I/O 的应用程序的原因是,管理异步执行函数的上下文非常容易。
The closures are a way to execute code in the context it was created in.
What this means for concurency is that you can define variables, then initiate a nonblocking I/O function, and send it an anonymous function for its callback.
When the task is complete, the callback function will execute in the context with the variables, this is the closure.
The reason closures are so good for writing applications with nonblocking I/O is that it's very easy to manage the context of functions executing asynchronously.
两个很好的例子是关于如何管理模板和使用渐进增强功能。您只需要一些轻量级的 JavaScript 代码即可使其完美运行。
我强烈建议您观看并阅读这些文章:
选择任何语言并尝试记住如何管理 HTML 文件模板以及更新单个CSS DOM 中的类名称结构(例如,用户单击菜单项,您希望将其标记为“已选择”并更新页面内容)。
使用 Node.js,就像在客户端 JavaScript 代码中执行一样简单。获取 DOM 节点并将 CSS 类应用到该节点。获取 DOM 节点和内容的 innerHTML(您将需要一些额外的 JavaScript 代码来执行此操作。阅读本文以了解更多信息)。
另一个很好的例子是,您可以使用同一段代码使您的网页与打开或关闭的 JavaScript 兼容。想象一下,您有一个用 JavaScript 进行的日期选择,允许您的用户使用日历选择任何日期。您可以编写(或使用)同一段 JavaScript 代码,使其在打开或关闭 JavaScript 的情况下工作。
Two good examples are regarding how you manage templates and use progressive enhancements with it. You just need a few lightweight pieces of JavaScript code to make it work perfectly.
I strongly recommend that you watch and read these articles:
Pick up any language and try to remember how you would manage your HTML file templates and what you had to do to update a single CSS class name in your DOM structure (for instance, a user clicked on a menu item and you want that marked as "selected" and update the content of the page).
With Node.js it is as simple as doing it in client-side JavaScript code. Get your DOM node and apply your CSS class to that. Get your DOM node and innerHTML your content (you will need some additional JavaScript code to do this. Read the article to know more).
Another good example, is that you can make your web page compatible both with JavaScript turned on or off with the same piece of code. Imagine you have a date selection made in JavaScript that would allow your users to pick up any date using a calendar. You can write (or use) the same piece of JavaScript code to make it work with your JavaScript turned ON or OFF.
有一个非常好的快餐店类比,它最好地解释了 Node.js 的事件驱动模型,请参阅全文,Node.js、医生办公室和快餐店 – 了解事件驱动编程
这里是一个总结:
Node.js 是事件驱动的,但大多数 Web 服务器都是基于线程的。York 解释了 Node.js 的工作原理:
您使用 Web 浏览器在
Node.js Web 服务器。
Node.js 服务器接受您的请求并调用函数来检索
该文件来自磁盘。
当 Node.js 服务器等待检索文件时,它
服务下一个 Web 请求。
当文件被检索时,有一个回调函数
插入到 Node.js 服务器队列中。
Node.js 服务器执行该函数,在本例中将
渲染“/about.html”页面并将其发送回您的 Web 浏览器。”
There is a very good fast food place analogy that best explains the event driven model of Node.js, see the full article, Node.js, Doctor’s Offices and Fast Food Restaurants – Understanding Event-driven Programming
Here is a summary:
Node.js is event-driven, but most web servers are thread-based.York explains how Node.js works:
You use your web browser to make a request for "/about.html" on a
Node.js web server.
The Node.js server accepts your request and calls a function to retrieve
that file from disk.
While the Node.js server is waiting for the file to be retrieved, it
services the next web request.
When the file is retrieved, there is a callback function that is
inserted in the Node.js servers queue.
The Node.js server executes that function which in this case would
render the "/about.html" page and send it back to your web browser."
嗯,我明白
对我来说,这意味着你的所有三个假设都是正确的。这个图书馆看起来确实很有前途!
Well, I understand that
For me that means that you were correct in all three assumptions. The library sure looks promising!