简体   繁体   English

为什么使用 Node JS 来创建 REST API 和 MVC Web 应用程序

[英]Why is Node JS being used to create REST APIs and MVC Web Applications

I am learning NodeJS and have tried to create few examples with it.我正在学习 NodeJS 并尝试用它创建一些示例。 The tutorials, blogs or videos I followed for this purpose some how ended up into creating a web applications with Express and Mongo db, hence I too was led into that directions.为了这个目的,我遵循的教程、博客或视频最终使用 Express 和 Mongo db 创建了一个 Web 应用程序,因此我也被引导到了这个方向。

Now one of the most iterated sentence during this time I read was that Node is much better with handling events and asynchronous programming, so an event based application can surely utilize the facilities of Node, just like a Chat Server (as there might not be enough of processing to be done).现在我读到的这段时间里重复最多的一句话是 Node 在处理事件和异步编程方面要好得多,因此基于事件的应用程序肯定可以利用 Node 的功能,就像聊天服务器一样(因为可能没有足够的要进行的处理)。 But these applications were very few.但这些应用程序很少。

Now I am little curious to understand on how creating a web application(or REST APIs) utilizes the goodness of events.现在,我对如何创建 Web 应用程序(或 REST API)利用事件的好处有点好奇。 Is there something that I have missed while understanding Node and are there other applications of Node in other than Chat Servers?我在理解 Node 时是否遗漏了一些东西,除了聊天服务器之外,还有其他 Node 应用程序吗?

https://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js https://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js

It have some examples of Where Node.js Should Be Used like,它有一些 Node.js 应该被使用的例子,比如,

CHAT聊天

API应用程序接口

QUEUED INPUTS排队输入

DATA STREAMING数据流

PROXY代理

There can be many more where asynchronous nature can be used.可以使用更多异步特性的地方。

Let me stick on the API/WebServer example you've come up with.让我坚持您提出的 API/WebServer 示例。

As Node.js basically runs JavaScript code it is inherently asynchronous, obviously.由于 Node.js 基本上运行 JavaScript 代码,它本质上是异步的,很明显。 So what this basically means is that it tries to offload any expensive or long running tasks (such as disk I/O or database requests) to "someone else" (eg Kernel, native db libs).所以这基本上意味着它试图任何昂贵或长时间运行的任务(例如磁盘 I/O 或数据库请求)卸载到“其他人”(例如内核、本地数据库库)。 Once it has done that it can continue working on other tasks.完成后,它可以继续处理其他任务。 Hence it is non blocking .因此它是非阻塞的

Now think of a webserver serving your static html, js,... files and providing an API.现在考虑一个为您的静态 html、js、... 文件提供服务并提供 API 的网络服务器。 Basically lot of the work your server will do is stuff like "serve me that file" (=disk I/O) or (in case of the API) fetch me some data from the database.基本上,您的服务器要做的很多工作是“为我提供该文件”(=磁盘 I/O)或(在 API 的情况下)从数据库中获取一些数据。 As those tasks are not executed in your node environment your node application itself (assuming that you dont have other bottlenecks) is able to handle a lot of requests.由于这些任务不在您的节点环境中执行,因此您的节点应用程序本身(假设您没有其他瓶颈)能够处理大量请求。

Additionally node is single threaded.另外节点是单线程的。 It simply does not need multiple threads for standard use cases as you do not have the issues, like in other languages, that a thread will block for a longer period of time.对于标准用例,它根本不需要多个线程,因为您没有像其他语言一样,线程会阻塞更长时间的问题。 This makes it a lot easier to work with in my opinion as you do not have to deal with all the problems that come with paralell programming.在我看来,这使得它更容易使用,因为您不必处理并行编程带来的所有问题。 However when it comes to CPU intensive tasks, which are relatively rare in the web server and API area, this might be an issue (which is solvable tho).然而,当涉及到 CPU 密集型任务时,这在 Web 服务器和 API 领域相对较少,这可能是一个问题(这是可以解决的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM