DEV Community

Cody Daigle
Cody Daigle

Posted on

The Wonders of Node.js

Node.js is a single thread, open source, cross-platform runtime environment, JavaScript interpreter that is or will be a vital tool to help anyone from an expert to beginner. In this article I will lay down some of it's foundational traits and capabilities to help you grow and understand the functionality that it possesses.

Image description

A Little History

Prior to the creation and implementation of Node.js, servers would struggle with high volume concurrent connections. Due to this issue code would either need the use of multiple stacks or imply blocking on entire processes. As you could imagine this could have a serious impact on businesses and their ability to meet a high volume of users requirements when engaging with their products. In 2009 Ryan Dahl created Node.js for that purpose. Although it was originally for Linux and Mac users it was later developed to be utilized on Windows as well.

What is Node.js?

Scripture defines Node.js as: An open-source, cross-platform runtime environment that allows developers to create all kinds of server-side tools and applications in JavaScript. The runtime is intended for use outside of a browser context (i.e. running directly on a computer or server OS). What makes Node so performant is it allows the developer to run the V8 JavaScript engine, which is the core of Chrome, outside of the browser. That may not seem exciting right now, but this is just the tip of the iceberg!
Some other JS engines would be:

Node.js comes equipped with a standard library, including a set of asynchronous input/output primitives, that prevents the blocking operation for JavaScript. Generally, Node.js libraries are created using non-blocking paradigms so any blocking is considered an exception.

Asynchronous vs Synchronous
Just as I mentioned before, Node is an Asynchronous I/O and it allows the utilization of a handling input/Output operations that does not perform blocking on processes. Essentially this action performs an action on data, or a resource, and returns expedited without the need to wait for a response from elsewhere. It's primarily used when a developer wants to perform an action, but doesn't require immediate interaction with the application or resource they are involved with. The multitasker or dedicated user's tool that allows you to work on multiple threads at once without being hung up. However, there are some restrictions that come with asynchronous I/O if being used for a network communication. For example if any other part of an application being worked on is dependent on having 'reads' or 'writes' then they must be completed before those can take place or else it can cause issues later along the line when running load conditions.
Now, a synchronous I/O it the opposite. It's a technique in which processes are blocked until the operation is completed. I like how GeeksForGeeks describe it:

This can happen when there is no way for processes to continue until their scheduled event has finished. In this case, synchronous I/O needs to wait patiently till it has been given permission by its supervisor program to proceed with further processing.

I did mention that Node.js has some blocking behaviors in it's arsenal. For example if JavaScript is exhibiting poor performance from something like being CPU intensive, instead of waiting on an operation, like I/O, it's generally referred to as blocking. This synchronous method is the standard library uses Libuv.

Why use it?

So we've talked about what Node is, but why use it?
Aside from the layout that makes up Node.js it has a magnitude of uses. When talking about it's functionality with blocking it opens the door to handle thousands of concurrent connections with just a single server and the thought of managing thread concurrency. Ultimately avoiding a potentially large source of bugs.
Furthermore, Node brings a unique advantage to front end developers that write JavaScript for the browser. Instead of learning an entirely new language this advantage allows them to write server-side as well as client-side code. With those same developers in mind Node.js has the capability to switch between it's ECMAScript versions so they won't have to rely on the users to update their browser in order for them to perform actions on it.
Node.js recommends these before going in headfirst:

  • Lexical Structure
  • Expressions
  • Data Types
  • Classes
  • Variables
  • Functions
  • this operator
  • Arrow Functions
  • Loops
  • Scopes
  • Arrays
  • Template Literals
  • Strict Mode
  • ECMAScript 2015 (ES6) and beyond
//Example of server-side Node syntax
const http = require('http');

const host = '127.0.0.1';
const port = 2000;

const server = http.createServer((request, response) => {
reponse.statusCode = 200;
response.setHeader('Content-Type', 'text/plain');
response.end('this is an example of Node.js on a local computer.');
});

const listener = `Node.js server running on http://${host}:${port}/.`
server.listen(port, host. () => {
console.log(listener);
});
Enter fullscreen mode Exit fullscreen mode

I hope you found this information useful and has you ready to work with Node.js. Aside from it's capabilities and functionality there are other assets that are involved and will ultimately help you in the ways you need.

Top comments (0)