node.js有什么好处_什么是Node.js? 它为什么如此重要? 有什么好处?

node.js有什么好处

什么是Node.js? (What is Node.js?)

Node.js是一种类似于PHP或ASP的服务器端脚本语言,但是用于实现HTTP Web服务器和应用程序框架的完整软件包。 区别在于Node.js的执行引擎是 asynchronous and event driven, which throws a whole new light on the way a server side app performs. As Node.js is the complete package (web HTTP server and server side language), there is dramatically less overhead in implementing it as a webserver when compared with a typical Apache (and PHP) or IIS (with ASP/PHP) installation.

Node.js is written in JavaScript, the most common client-side language around. This reduces the learning curve somewhat for most web developers as they understand JavaScript and how to code it.

Node.js用JavaScript编写,JavaScript是周围最常见的客户端语言。 对于大多数Web开发人员来说,这可以减少学习曲线,因为他们了解JavaScript以及如何对其进行编码。

为什么重要,有什么好处? (Why is it Important and what are the Benefits?)

我使用的大多数服务器端语言都建立在 sequential execution models, meaning one task has to finish before another one begins. For instance, PHP (in conjunction with the type of server it is running off e.g. Apache) will spawn a thread for each request made to the server. If that thread needs to get data from a database that takes a while or another request comes in, that thread is locked up and consuming resources. For a data intensive app with many concurrent connections, that could severly impede the server’s performance.

Node.js is single threaded (yes only one thread!) and is beneficial in applications where frequent blocking I/O requests are made that rely on another part of the server system to complete the action before it can continue.  

Node.js是单线程的(仅一个线程!),在频繁发出阻塞I / O请求的应用程序中很有用,这些请求依赖于服务器系统的另一部分来完成操作,然后才能继续。

Node.js, has only the single thread that processes each request as it comes in. That is by design. When the thread needs to do some kind of action that could potentially lock up the system, Node.js initiates that request with a callback for when the data is returned and releases the resources it is using to maintain that request. When the data is returned, the callback is initiated and the process continues. In the meantime Node.js (and the server) has been able to service other requests and perform other actions. This in turn prevents unnecessary CPU cycles waiting for the data, let alone preventing any kind of performance issue due to a lack of resources.

Node.js仅具有处理每个请求的单线程。 这是设计使然 。 当线程需要执行某种可能会锁定系统的操作时,Node.js会使用回调函数启动该请求,以获取返回数据的时间,并释放用于维护该请求的资源。 返回数据后,将启动回调并继续执行该过程。 同时,Node.js(和服务器)已经能够处理其他请求并执行其他操作。 反过来,这避免了不必要的CPU周期等待数据,更不用说防止由于资源不足而导致的任何类型的性能问题。

In an example of a web application receiving data from a web form, there may be many actions that need to be done based on that data. One such action may be to check if the data is valid by comparing it against logic defined by the programmer. For simple applications, this may be done all in the script, in which case Node.js is no more beneficial than any other server side language. However, the real benefit of Node.js comes when that server side script needs to interface with another system such as a database, a disk, another network or a combination of these, where the script would otherwise sit and wait (consuming server resources) for that particular system to do its thing before returning the data.  

在Web应用程序从Web表单接收数据的示例中,可能需要基于该数据执行许多操作。 一种这样的动作可以是通过将数据与程序员定义的逻辑进行比较来检查数据是否有效。 对于简单的应用程序,这可以全部在脚本中完成,在这种情况下,Node.js不会比任何其他服务器端语言更有用。 但是,当该服务器端脚本需要与另一个系统(例如数据库,磁盘,另一个网络或这些的组合)进行交互时,Node.js的真正好处就出现了,否则脚本将处于等待状态(消耗服务器资源)让该特定系统在返回数据之前先行其事。

So continuing the example of the data from a web form, the data now contains a username and password that needs to be validated against user data in a database. Node.js, as mentioned before, uses an event driven model via the callback. When the request is made to the database to compare the username and password, Node.js releases the action, sets the callback and continues on to the next line of code in your app. When the database completes execution of that particular query and returns the data, the script jumps to the callback and executes the code contained within it.

因此,以Web表单中的数据为例,该数据现在包含一个用户名和密码,需要针对数据库中的用户数据进行验证。 如前所述,Node.js通过回调使用事件驱动的模型。 当请求数据库比较用户名和密码时,Node.js释放操作,设置回调并继续执行应用程序中的下一行代码。 当数据库完成该特定查询的执行并返回数据时,脚本将跳转到回调并执行其中包含的代码。

The example below (using semi-pseudo code) demonstrates how Node.js works. It's more than likely that assuming a valid username AND password, the output would be:

下面的示例(使用半伪代码)演示了Node.js的工作方式。 很有可能假设有效的用户名和密码,输出将是:

Your username is OK!

Correct Password!

This is due to Node.js not waiting around until the database has finished the query and has moved onto the next line of code (checking the username).

这是由于Node.js直到数据库完成查询并移至下一行代码(检查用户名)后才等待。

// validate the username and password

// query the database
$db->query("check passwords;", function() {
   if ($password is not Valid) {
       alert('Incorrect password');
   }
   else {
       alert('Correct Password!');
   }
});

// check the username is valid
if ($username is not Valid) {
   // error here
   alert('There is an error with your username');
}
else {
   alert('Your username is OK!');
}

有哪些障碍? (What are the Roadblocks?)

最大的问题之一是许多主机尚不支持Node.js,这意味着您需要通过Digital Ocean,Amazon Web Services和其他PaaS(平台即服务)实体来管理自己的主机实例。 必须照顾自己的服务器的安全性让我一直感到不舒服,这就是为什么我总是选择一台为我做这件事的主机的原因。 我认为值得付出。

The learning curve makes it a difficult language to pick up and run with. 

学习曲线使其成为难以掌握和使用的语言。

The whole flow of your application will be affected by the way Node.js works. It isn’t always obvious how your code is going to execute and in what order, which may lead to situations you may want to avoid such as nesting all your logic into one event callback after another.

您的应用程序的整个流程将受到Node.js工作方式的影响。 并非总是很清楚代码将如何执行以及执行顺序如何,这可能会导致您可能想要避免的情况,例如将所有逻辑嵌套到一个事件回调中。

模块化方法 (A Modular Approach)

我希望将Node.js用作我的Web服务器,但是我已经习惯了框架和插件随如PHP之类的语言,因此我发现要使功能完善的Web服务器具有基本的路由和其他与Web服务器相关的通用过程相当费力对我正在构建的网站不可见。 我不想知道服务器如何仅用于构建基本站点。 我坚信模块化方法,因此我改用Sails.js为我做到了。 Sails.js基于MVC方法,非常容易建立站点,而无需编写自己的服务器。

摘要 (Summary)

Node.js的重要方面是确定它是否适合您的应用程序,并且在任何性能方面都将超过实现成本,从而使您受益。 由于有限的Web主机支持,这通常意味着您自己托管和维护服务器(或向其他人付费)的成本较高,但是如果您的站点需要如上所述的强大的数据I / O功能,并且您想要整个网站包,那么Node.js就适合您。

翻译自: https://www.experts-exchange.com/articles/18420/What-is-Node-js-Why-is-it-Important-What-are-the-Benefits.html

node.js有什么好处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值