A Beginner’s Guide to Node.js

nodejsjavascript

Friday, August 18, 2023

I’ve used Node.js in several projects for my back-end alongside React for my front-end. While working on a project recently I became curious about how Node.js works and realized that while I know how to use Node.js I really didn’t know why it works the way it does. I wanted to learn more and decided to write a short and simple blog on the history behind its implementation and a high-level overview of how it works for beginners.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that is used for running web applications outside the client’s browser.

Open-source — the program is made freely available for developers to use and contribute to.

Cross-platform — it can be run on multiple operating systems — Windows, OSX, and Linux.

Runtime Environment — the Runtime environment of any programming language is the environment where you can execute the code written in that language. Before Node.js JavaScript was only executed in the browser. Node.js enables you to execute JavaScript code without a browser making it possible to use JavaScript for server-side scripting.

A Brief History:

Node.js was initially developed by Ryan Dahl who criticized the blocking I/O nature of the most popular web server at the time, Apache HTTP server and the complex dependency library of Nginx, Apache’s rival in the web server space.

One of his biggest arguments against Apache was the memory usage required as the number of concurrent requests increased. Apache is multi-threaded and was configured so that each thread could handle tasks independently of each other while sharing the same resource. However, each task requires a separate execution context which takes up memory and could lead to slower response times as the number of concurrent requests increases.

Node.js introduced a lightweight, event-driven, JavaScript run-time environment. Much like the browser JavaScript runtime environment, Node.js utilizes an event loop and callbacks to delegate tasks that take a while to a separate component while continuing down the thread of execution. Additionally, Node.js had fewer dependencies making it lightweight and responsive.

Node.js was later sponsored by Joyent after Ryan Dahl gave a presentation on the new technology and its capabilities at the JSConf in 2009.

Ryan Dahl was not the first person to conceive of the idea of server-side programming, Netscape which created the earliest rendition of JavaScript had a runtime environment called Netscape LiveWire which allowed users to use JavaScript to create server-side applications. However, the technology was not successful partly because JavaScript was also not as widely used at that time. Node.js’s release came at perfect timing as JavaScript was being considered more seriously thanks to modern web browsers utilizing the technology to create dynamic websites.

How Node.js works:

Although Node.js is a JavaScript runtime environment it was primarily written in C/C++ which has many features that enable it to interact with machines operating systems directly.

Node.js is comprised of several dependencies but the two primary ones are V8 engine and libuv. V8 is developed by Google and it compiles JavaScript code directly into C++ code providing a runtime environment for the execution of JavaScript completely independent of the browser on which it runs. Libuv is a multi-platform C++ library that provides support for asynchronous I/O-based operations such as file system, networking, and concurrency and manages the event loop in Node.js.

Through Libuv, Node.js enables us to create non-blocking asynchronous code for our backend applications by leveraging an event loop so that the program can continue to execute while it waits for tasks that may take longer to complete like HTTP requests or receiving data from a database.

Once you run Node.js the program begins by executing the code synchronously and only once it has executed the input script does it enter the event loop to execute any asynchronous API callbacks. The event loop determines the order in which the callbacks will be executed and consists of different queues that handle macro/micro tasks in order of priority. The event loop is concealed from the user, similar to the browser runtime environment.

Pros and Cons of Node.js:

Pros:

Asynchronous, event-driven I/O model making it lightweight and highly responsive

The front-end and back-end of applications can be written in JavaScript which can help companies save on time and expenses by hiring full-stack developers vs. requiring separate teams for front-end and back-end.

Powered by V8 engine, which is consistently being improved and invested in by Googe.

Cons:

Performance is impacted by CPU intensive tasks like complex calculations or video compression which take a while to complete and will block the main thread. Although Node.js can handle concurrent tasks by leveraging an event loop it was designed to handle callbacks that would not take a long time to resolve.

Node.js is constantly implementing changes to it’s API sometimes making it incompatible with existing projects. Developers are having to spend time on rewriting their programs to make them compatible with these changes instead of allocating that time to work on improvements.

Summary:

Node.js is an extremely lightweight highly scalable program for backends currently used by many large companies including but not limited to Netflix, PayPal and Walmart which makes it highly suitable for a wide range of projects.

Sources:

https://heynode.com/tutorial/how-event-loop-works-nodejs/#:~:text=The%20event%20loop%20is%20a,tutorial%20is%20useful%20for%20Node.

https://bytearcher.com/articles/parallel-vs-concurrent/

https://www.geeksforgeeks.org/node-js-vs-express-js/

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction

https://www.netguru.com/glossary/node-js#:~:text=Before%20Node.,teams%20for%20backend%20and%20frontend

https://medium.com/nerd-for-tech/nodejs-short-history-and-milestones-b1708a0c6488

https://www.codecademy.com/article/introduction-to-javascript-runtime-environments

https://www.geeksforgeeks.org/explain-the-working-of-node-js/

https://www.freecodecamp.org/news/what-exactly-is-node-guide-for-beginners/

https://dev.to/_staticvoid/node-js-under-the-hood-1-getting-to-know-our-tools-1465

https://resources.risingstack.com/Node.js+at+Scale+Vol.+2+-+Node.js+Under+the+Hood.pdf

https://www.builder.io/blog/visual-guide-to-nodejs-event-loop