简体   繁体   中英

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

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). 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. Is there something that I have missed while understanding Node and are there other applications of Node in other than Chat Servers?

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,

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.

As Node.js basically runs JavaScript code it is inherently asynchronous, obviously. 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). 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. 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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