DEV Community

Cover image for Everything you need to know about Node.js

Everything you need to know about Node.js

Jorge Ramón on July 05, 2019

Node.js is one of the most popular technologies nowadays to build scalable and efficent REST API's. It is also used to build hybrid mobile applicat...
Collapse
 
shmdhussain12 profile image
Mohamed Hussain

Thanks for this clear article on how the things are done in background using nodejs....I have some question : what is the difference between worker threads and os threads....in prime number example why os threads are not used by default by libuv when doing CPU heavy task...what is the limit for the os threads...

Collapse
 
pierreyveslebrun profile image
Pierre Lebrun

As said in the article, your code must be written in C++ in order to be able to use the OS threads pool.

Hence the introduction of worker threads which can be used directly in JS.

Collapse
 
jorge_rockr profile image
Jorge Ramón

Yeah, he is right :D.

That's why DB drivers are written in C++.

Collapse
 
karataev profile image
Eugene Karataev

Thanks for the great post!

Let's say I need a worker to do heavy CPU calculations. How does the worker works under the hood? Does it start on a separate CPU core and uses this core by 100%?
What if a CPU has only one core? Will Node worker help with this case or it'll be useless to start a worker, because there are no free resources to split between the main thread and worker thread?

Collapse
 
jorge_rockr profile image
Jorge Ramón

Thanks for commenting.

Node.js by default works in a single CPU core, so worker thread will spawn and execute in the same CPU core that the main thread does.

If you want to deploy a Node.js application across all CPU cores you need to write some code using cluster native module. Thanksfully there is a library called PM2 which does the dirty work for you and deploys a Node.js application in all CPU cores with its built in load balancer.

The worker thread real problem is spawn like crazy, since creating a subthread (worker thread) isn't cheap (talking about CPU time and resources) but it's cheaper than fork the same process.

Collapse
 
karataev profile image
Eugene Karataev

Well, if a separate worker and main thread are executed in one CPU core, then they share the same resource.
Is it correct that if we write heavy CPU code in "chunks", i.e. we will return execution context from a heavy function to the function which handles http requests with some interval, then we will emulate workers? But in this case it's not necessary for Node to allocate resources for a subthread, i.e. it's cheaper in terms of performance.

Thread Thread
 
jorge_rockr profile image
Jorge Ramón

Remember Worker as OS thread actually so I don't think they will be emulated in a single CPU core case. Anyway that's an interesting question but unfortunately workers are pretty "new" so there isn't enough information about them.

Collapse
 
kp profile image
KP

@jorge_rockr amazing article, i learned a lot about nodejs.
Question: I am building a mobile-responsive website, and my stack has Nuxt.js (vue.js framework) for the front end, talking to back-end APIs in Laravel (php framework). Nuxt.js is built on top of node (I think?) and I am using it for server-side-rendering. What are your thoughts on this architecture, versus using something purely in node.js?

Collapse
 
jorge_rockr profile image
Jorge Ramón

Hi! Thanks you so much for reading my article.

Yeah, Nuxt.js and Next.js (React) are built on top of Node.js and it's cool since you can use your Frontend knowledge adding some Backend code.

In fact, using pure Node.js could have a better performance but it could be more complex and hard to maintain.

Collapse
 
kp profile image
KP

@jorge_rockr it was a phenomenal article, thank you for writing it.

Thanks for the recommendation. Sounds like I should stick with back-end APIs in Laravel, and use Nuxt for my front end. Hopefully I should see a lot of the user-benefits one can get from using Node with server-side-rendering for SEO.

Collapse
 
al_karlssen profile image
Al Karlssen

Node.js is also a variety of ready-to-use packages written for the Node.js, thanks to which we can easily connect to almost any service or database. The most common Node itechcraft.com/node-js/ integration dialect, however, is JSON, which speaks well with the NoSQL databases.

Collapse
 
technologycrowds1 profile image
Technology Crowds • Edited

Excellent article!! Great Job! Really helpful for those who are new to NODE.JS .I cleared my all doubts regarding Node.js. Get more information about Node.js here.

Collapse
 
marykrivokhat profile image
Mary Krivokhat

Jorge Ramón, thank you for this awesome article!)

The company I am working at, in January-February 2020 starts the open-source project for Node.js developers (microservices)!
Warm welcome🥳
Spectrum: spectrum.chat/yap?tab=posts (community chat)
GitBook: manual.youngapp.co/community-edition/ (docs)
Twitter: twitter.com/youngapp_pf (news)
GitHub: github.com/youngapp/yap (docs)
(click🌟star to support us and stay connected🙌)

Collapse
 
veselinastaneva profile image
Vesi Staneva

Thank you for sharing this awesome post! Node.js rocks🤟

My team just completed an open-sourced Content Moderation Service built Node.js, TensorFlowJS, and ReactJS that we have been working over the past weeks. We have now released the first part of a series of three tutorials - How to create an NSFW Image Classification REST API and we would love to hear your feedback. Any comments & suggestions are more than welcome. Thanks in advance! 😊

Collapse
 
noor_codes profile image
Noorullah Ahmadzai

Wow! Amazing Article!
Thanks for sharing ❤️

Collapse
 
maricaya profile image
Maricaya

Thanks for the great post!

Can I translate this article into Chinese so that more people can see it ?

Collapse
 
jorge_rockr profile image
Jorge Ramón

Sure!!

Collapse
 
oliverzulett profile image
Oliver Zulett

Increible explicacion antes de este post no tenia ni la mas remota idea de como funcionaba Node.js en realidad, excelente

Collapse
 
jorge_rockr profile image
Jorge Ramón

De nada!

Collapse
 
jodyshop profile image
Waleed Barakat

Yes, it's very powerful, but how to build dynamic sites that uses mysql db like PHP?

Collapse
 
jorge_rockr profile image
Jorge Ramón

If you want to build a site in a PHP style you need a template engine.

There are many templates engines out there. It's better to use Express Framework and any of the following templates engines:

expressjs.com/en/guide/using-templ...

But if you really love PHP you can use it with Express Framework too:

npmjs.com/package/php-express

Collapse
 
jodyshop profile image
Waleed Barakat

Thank you :)

Collapse
 
daniellehutchings profile image
Danielle Hutchings

As someone who is new to coding and just beginning to learn Node.js, this article was super helpful to get an overview of how Node.js works and why it's beneficial. Thanks for sharing!

Collapse
 
jorge_rockr profile image
Jorge Ramón

Welcome! I'm glad it was useful for you

Collapse
 
devjeetroy98 profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Devjeet Roy

You might also like to visit : learnnodeonline.blogspot.com/
More blogs are about to come.

Collapse
 
ogaston profile image
Omar Gaston Chalas

Amazing! I'm surprised about how much you know about the internal process of Node.js, Can you suggest me some resources (aside of documentation) to learn advanced concepts?

Collapse
 
jorge_rockr profile image
Jorge Ramón
Collapse
 
ogaston profile image
Omar Gaston Chalas

Thanks Jorge, i'm checking them out right now!

Collapse
 
koolb profile image
Buddhima Wickramasinghe

Thanks for the great article.

Collapse
 
ochoadev profile image
Luis A. Ochoa R.

Niceeeee post!

Collapse
 
bacchusplateau profile image
Bret Williams

Nice overview Jorge. Question for you - with the advent of asynch enabled REST endpoints in .NET, specifically Web API, does this mitigate the C10M problem you referred to somewhat?

Collapse
 
jorge_rockr profile image
Jorge Ramón • Edited

According to Microsoft's official documentation:

For example, in Windows an OS thread makes a call to the network device driver and asks it to perform the networking operation via an Interrupt Request Packet (IRP) which represents the operation. The device driver receives the IRP, makes the call to the network, marks the IRP as "pending", and returns back to the OS. Because the OS thread now knows that the IRP is "pending", it doesn't have any more work to do for this job and "returns" back so that it can be used to perform other work.
When the request is fulfilled and data comes back through the device driver, it notifies the CPU of new data received via an interrupt. How this interrupt gets handled will vary depending on the OS, but eventually the data will be passed through the OS until it reaches a system interop call (for example, in Linux an interrupt handler will schedule the bottom half of the IRQ to pass the data up through the OS asynchronously). Note that this also happens asynchronously! The result is queued up until the next available thread is able to execute the async method and "unwrap" the result of the completed task.
Throughout this entire process, a key takeaway is that no thread is dedicated to running the task. Although work is executed in some context (that is, the OS does have to pass data to a device driver and respond to an interrupt), there is no thread dedicated to waiting for data from the request to come back. This allows the system to handle a much larger volume of work rather than waiting for some I/O call to finish.

Please note there is no thread dedicated to waiting for data from the request to come back, so that means they are not using Reactor Pattern (i.e. an Event Loop), they are really using purely OS resources (threads and interrupts) and that is awesome!!

I don't know if that's enough to solve C10M problem, maybe with big server resources such as 128 RAM, etc.

Collapse
 
maricaya profile image
Maricaya

Thanks for the great post!

Collapse
 
ijebusoma profile image
Vanessa Osuka

thanks for explaining worker threads more clearly!