无法理解node.js [英] Cannot understand node.js

查看:110
本文介绍了无法理解node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从没有JS经验的人那里,你对学习Node.js的建议是什么?

From someone with few experience in JS, what do you recommend for learning Node.js?

我在论坛上看了很多关于事件驱动的内容,非阻塞,异步,回调等,但我不知道那是什么!

我在哪里可以学习基础知识,以便理解所有条款在将来,node.js?

Where can I learn the basics in order to understand all that terms and in the future, node.js?

谢谢!

推荐答案

您提到的概念(事件驱动,非阻塞,异步,回调)并非特定于JavaScript,在更一般的上下文中理解它们是有价值的。它们都围绕着优雅地处理我们无法控制的资源。

The concepts you mention (event-driven, non-blocking, async, callbacks) aren't specific to JavaScript and understanding them in a more general context is valuable. They all revolve around gracefully handling resources over which we have no control.

想象一下,等待来自TCP连接的数据,等待操作系统删除文件,或等待用户点击按钮。如果您按照一步一步的方式对其进行编程(逐步同步),您就可以巡航 - 执行步骤1,执行步骤2,执行步骤3 - 直到你迈出等待某事发生的步骤。此时,您的程序将停止并拒绝让步,直到它收到数据,收到删除确认或收到按钮单击。换句话说,调用阻止程序继续进行。考虑到可能需要我们注意的其他TCP连接,文件操作和UI操作并且不依赖于我们正在等待的项目,这是非常低效的。

Imagine waiting for data from a TCP connection, waiting for the OS to delete a file, or waiting for a user to click a button. If you programmed this in a step-by-step fashion (step-by-step is synchronous), you'd cruise along - "do step 1", "do step 2", "do step 3" - until you hit the step "wait for something to happen". At that point, your program would stop and refuse to budge until it received the data, received delete confirmation, or received the button click. In other words, the call blocks the program from proceeding. This is pretty inefficient considering there are likely other TCP connections, file operations, and UI actions that need our attention and don't depend on the item we're waiting for.

在许多情况下,最好表明我们对资源感兴趣,并在资源发生变化时接收逐步说明之外的通知。从您的概念列表中:

In many cases, it would be better to indicate we're interested in a resource and receive notifications outside of step-by-step instructions when the resource changes. From your list of concepts:


  • 事件是我们感兴趣的资源的变化 - 我们的TCP连接收到了一些数据,文件删除完成,或者用户点击了一个按钮。

  • 异步调用告诉操作系统或运行时我们有兴趣做某事有资源。它们非阻塞 - 我们的程序可以在等待资源更改时处理其他内容。

  • 回调是资源发生变化时要执行的功能。异步资源调用通常接受对回调函数的一个或多个引用(一个用于成功,一个用于错误等)。当资源发生变化时,运行时会调用相应的回调。

  • Events are changes in the resources we're interested in - our TCP connection received some data, the file delete is complete, or a user clicked a button.
  • Asynchronous calls tell the OS or runtime that we're interested in doing something with a resource. They are non-blocking - our program can work on something else while it waits for a change in the resource.
  • Callbacks are functions to be executed when the resource changes. An asynchronous resource call often accepts one or more references to callback functions (one for success, one for an error, etc...). When the resource changes, the runtime calls the appropriate callback.

我们可以通过用node.js重命名文件来看到这些概念:

We can see these concepts illustrated by renaming a file with node.js:

var fs = require('fs');

// args (current file name, new file name, callback function)
fs.rename('/tmp/hello', '/tmp/world', function (err) {
  // this occurs when the rename is complete
  if (err) throw err;
  console.log('rename complete');
});
console.log('step after rename');

第三个参数可能看起来很奇怪。它是一个未命名的(匿名)函数,将在重命名完成后调用。

The third argument may look strange. It's an unnamed (anonymous) function that will be called when the rename is complete.

请注意,由于fs.rename是异步的,因此无法判断我们是否会首先看到重命名完成或重命名后消息。这是事件驱动/异步编程的缺点 - 如果我们有一组复杂的相互依赖的任务,我们需要非常小心地确保依赖任务在依赖它们的任务之前完成。异步调用完成顺序可以改变的事实可能导致非常微妙的错误。

Note that since fs.rename is asynchronous, it's impossible to tell if we'll see the 'rename complete' or 'step after rename' message first. That's the downside to event-driven/asynchronous programming - if we have a complex set of interdependent tasks, we need to be extremely careful to insure dependent tasks complete before the tasks that depend on them. The fact that the order of async call completion can change can lead to very subtle bugs.

参见:

  • An event-driven programming tutorial
  • Closures - more complicated manipulation of first-class functions

根据donald的要求编辑:

Edit per donald's request:

理解node.js的最佳方法是下载,构建,安装和使用它。您需要:

The best way to understand node.js is to download, build, install, and use it. You'll need:


  • Mac OS或Linux。如果您对Cygwin感到满意,那也可能是一个选项,但如果您运行的是Windows,我发现在虚拟机中运行Linux会更容易。

  • Git - 不是必需的,但它可以轻松获取代码库。

  • 一种调试应用程序的方法。请参阅此问题。最初,将调试信息写入控制台可能会起作用。最终,你需要强大的调试。

  • 一个想法 - 你想用node.js做什么?如果您对其功能概述感兴趣,请浏览其API

  • Mac OS or Linux. If your comfortable with Cygwin, that may also be an option but if you're running Windows I find it easier to run Linux in a virtual machine.
  • Git - not required but it makes fetching the code repository easy.
  • A way to debug your application. See this question. Initially, writing debug info to the console may work. Eventually, you'll want robust debugging.
  • An idea - what is it you want to do with node.js? If you're interested in an overview of its capabilities, browse its API.

大多数教程都关注node.js快速构建Http服务器的能力:

Most tutorials focus on node.js's ability to quickly build an Http server:

  • Going evented with Node.js (shows a simple version of downloading the repository, building, and installing)
  • A game lobby
  • An Http server with Html templating

请记住node.js填补了一个非常特殊的利基 - 它旨在构建网络程序。它可能不适合其他类型的程序。

Keep in mind that node.js fills a very particular niche - it's designed to build network programs. It may not be the right tool for other types of programs.

这篇关于无法理解node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆