Signup/Sign In

NodeJS Introduction

Node.js is nothing more than server-side JavaScript, and it's fantastic. We'll learn how to set it up and explain the important concepts in this post.

JavaScript is a single-threaded, dynamically typed interpreted language for the Web. That means that if you're working on a web page, you can use this language to execute operations on it, such as running JavaScript code when a user clicks a button.

Since it's single-threaded, the JavaScript code executes synchronously or sequentially line by line.

History

Ryan Dahl created Node.js in 2009, roughly thirteen years after Netscape's LiveWire Pro Web introduced the first server-side JavaScript environment. Only Linux and Mac OS X were supported in the initial version. On November 8, 2009, Dahl presented the project at the first European JSConf.

Server-side JavaScript

Since JavaScript is used on the internet, it must be stable. As a result, you can't reach the machine that JavaScript is running on, such as the File System, IO, Networking, and so on, and neither ECMAScript nor JavaScript has requirements for this.

As a result, it is up to browser vendors to add APIs to the JavaScript engine that can do other tasks. For instance, the DOM API is in charge of converting HTML code into actual pixels on the device.

These APIs are in charge of performing operations that JavaScript isn't built to handle. The browser has these APIs, which are referred to as Web APIs. These Web APIs often run their work in a different thread, allowing another JavaScript programming to continue to run as the job is being completed in the background. When the job is completed, it notifies the main JavaScript thread.

How Node.js works?

Node.js is a cross-platform, open-source back-end JavaScript runtime environment. Node.js allows developers to use JavaScript to create command-line tools and server-side scripting, which involves executing scripts on the server before sending the page to the user's device. The V8 JavaScript engine, the basic library of packages, and some binaries are all part of the Node.js project.

The event-driven model of Node.js allows for asynchronous I/O. These architecture choices are intended to improve the scalability of web applications that have a lot of input/output operations, as well as for real-time Web applications.

To execute low-level non-JavaScript time-taking operations, Node.js uses several threads. As a result, when a time-consuming process like reading a file is in progress, our JavaScript is not blocked.

Technical Details

Let's learn about few technical details which come with NodeJS.

Threading

Node.js uses a single thread event loop and non-blocking I/O calls to accommodate tens of thousands of simultaneous connections without incurring the overhead of thread context switching. In Node.js, a thread pool manages the execution of concurrent tasks. The default number of threads in the libuv thread pool can be increased by developers. These threads are likely to be distributed through several cores by the server operating system (OS).

V8

V8 is a JavaScript execution engine that was designed with Google Chrome in mind. At runtime, V8 compiles JavaScript source code to native machine code.

Package management

The package manager npm comes pre-installed for the Node.js server platform. It organizes the installation and maintenance of third-party Node.js applications by installing them from the npm repository.

Event loop

Node.js files with the operating system so that it can be notified of new connections and receive callbacks. Instead of processes or threads, Node.js scales by using an event loop. Unlike other event-driven servers, Node.js's event loop does not require explicit calling. When there are no further callbacks to be executed, Node.js leaves the event loop.

Native bindings

Node.js has a C-based API called N-API that can be used to build loadable (importable).node modules from C/C++ source code. Third parties have created open-source C/++ wrappers on top of the API to fix the issue, which only partially solves the problem. They simplify interfaces, but they can add confusion that maintainers must contend with as a side effect. About the fact that Node.js' main functionality is included in a JavaScript built-in library, C++ plugins can be used to extend the features and boost the performance of applications.

Conclusion

Now, that you have a piece of good knowledge about NodeJS, the main motive of this article was to bust the myth that NodeJs is complex, in fact, it's pretty easy and robust at execution. As the next step, we'll learn how to install NodeJS on your system so that you can go ahead and start creating and experimenting with NodeJS.



About the author: