Intro to Node.js

Node.js is a runtime built on Google Chrome’s V8 JavaScript engine (built with C++).

Traditionally, JavaScript could only be ran in the browser. Node took JavaScript out of the browser and allowed it to run on the machine. Files could be accessed, network traffic could be listened for, databases could be directly accessed. To accomplish these things previously, you would need to use a server-side language like PHP or Ruby on Rails.

Additionally, it is incredibly fast. I/O operations like file access and database access are offloaded to C++ APIs by the event loop. This allows JavaScript to run in the main thread without interruption.

In this post, we’ll take a deeper look at Node and the kinds of applications we can build with it.

Example Code

Example 1

As you can see, this is how easily you can create a basic server in Node.js. This is one of the major benefits of using it, having the ability to get something up and running with minimal effort. In comparison, Microsoft’s ASP.NET (.NET Core 3.1) web framework requires roughly 200 lines of boilerplate code spread across six files. Only up until recently, with the release of .NET 6, was it stripped to be minimal like Node.

Advantages

  • Full Stack JavaScript Development: One of the major benefits of Node is that it allows you to create full stack web applications all in JavaScript. This makes it much easier to be a full stack developer as long as you have a good understanding of JavaScript. It is much more efficient than having to context switch between frontend JavaScript and some backend technology like C#, PHP or Ruby on Rails. It is also easier to develop a deeper understanding and level of proficiency with JavaScript without having to juggle some other programming language as well.
  • Scalable: Node.js runs JavaScript in a single thread, meaning fewer resources are required to have thousands of concurrent connections on the same instance. Traditionally, a thread would be spawned or a new process would be forked each time a connection or incoming request was made to the server. That approach can be inefficient and requires a lot of overhead. In Node, each new connection causes a callback to fire to handle requests with non-blocking I/O calls. Any blocking I/O operations are allocated by the event loop to be handled by background threads (while Node runs JavaScript in a single thread, Node.js itself is multithreaded). Node’s approach requires less memory and handles more connections than other architectures that scale with threads such as Apache Server, ASP.NET, and Ruby on Rails.
  • Extensible: There is an NPM package manager included with the installation of Node. It is free to use and provides over 1.3 million packages of reusable Node.js code. It also manages dependencies. With a single command, all the dependencies in the package.json (the heart of any Node project) file are installed. If you require an earlier version of an npm package, you can install it easily through the command line. The major benefit here is that you can leverage code others have written without having to reinvent the wheel.
  • Quick Development Time: Node.js is so popular among startups because of how quickly it is to go from an idea to an application. With NPM packages like Express at your disposal, you can create a backend for your application with minimal code. Additionally, there is a quick feedback loop. With NPM packages like nodemon, your application will update in the browser after code changes are made. Compare this to ASP.NET where you have to recompile and rebuild the project after each change which could take minutes. Doing this many times a day adds up, not to mention losing momentum while waiting for a long build to complete. Deploying Node apps is also a breeze. You can make changes and see the results in production in seconds (could be a few minutes depending on your project). Finally, you can do everything in Node through the command line as opposed to clicking through a UI.

Disadvantages

As great as Node.js is, it is not without some shortcomings:

  • Maintainability Issues: Node applications can be difficult to maintain with all the third-party dependencies. When a package breaks, it can be difficult to debug.
  • Performs Poorly With CPU Intensive Tasks: Node performs excellently in many scenarios, but will not perform well with CPU intensive tasks. The event loop is single threaded so all other tasks will have to wait for the CPU intensive task to complete before they can be dispatched.

What Can You Build?

  • Internet of Things (IoT): Developing systems to handle large numbers IoT devices on a network has proven to be challenge. Node.js is able to process multiple concurrent requests and events emitted by thousands (sometimes millions) of devices on a network. This is all thanks to Node’s asynchronous event-driven architecture, which makes it great for developing IoT systems.
  • Full Stack Web Applications: As stated previously, full stack web applications can be built using JavaScript on the client-side and server-side. This allows for more efficient development because there is no context switching between languages. Also, a developer can work on any part of the application as long as they know JavaScript. If you would like to learn more about developing web applications with Node, check out my post here.
  • Real-Time Chat Applications: Node has an Event API that can create EventEmitter objects. These objects can be used to attach event handlers to your events. Node’s event-driven architecture combined with libraries like socket.io can be used to create fast, low latency real-time chat apps.
  • Streaming Apps: Node has a Stream API that can be used to create streaming applications. In fact, Netflix even uses it in their streaming software to deliver high volume streaming to over 182 million subscribers.

Conclusion

The Node.js hype has died down some but it remains a very powerful platform for creating fast, scalable and responsive applications.

Many large companies have implemented it with success. Paypal doubled their user requests per second due to decreased response time, reducing download time by 35%. Netflix reduced their startup times by 70%. LinkedIn, GoDaddy, Twitter, Trello and Uber are some other companies that have used it to increase performance. Startups also continue to use it over other options.

The hype may have died down a bit, but Node.js is still in demand as ever and there is plenty of opportunity for people versed in it.