Skip to content

Node.js

Starting from this chapter, we officially embark on the journey of backend development with JavaScript.

Node.js is a highly popular technology today, but its creation has a unique history.

As is well known, within months of Netscape designing JavaScript, it had already become the only standard for frontend development.

Later, after Microsoft defeated Netscape with Internet Explorer (IE) and unified the desktop market, browser development stagnated for years. Without competition, there is no progress. Microsoft believed that IE6 was already highly polished, with little room for improvement, and subsequently disbanded the IE6 development team! Meanwhile, Google believed that a new generation of browsers supporting modern web applications was just beginning, especially with the potential to enhance the performance of JavaScript engines by tenfold.

Mozilla, leveraging the now-defunct Netscape legacy, launched the Firefox browser in 2002. Shortly after, Apple introduced the Safari browser based on the open-source KHTML engine in 2003, but it was limited to the Mac platform.

Then, Google began creating its own browser. They saw the potential in the WebKit engine and launched Chrome based on it.

The Chrome browser was cross-platform for Windows and Mac, and Google believed that to run modern web applications, a browser needed a very powerful JavaScript engine. Thus, Google developed a high-performance JavaScript engine named V8, which was released as open source under the BSD license.

The browser wars left Microsoft's IE far behind, as they had disbanded their most experienced and capable browser team! In attempting to catch up, they found that WebKit, which supports HTML5, had already become the standard on mobile devices, isolating IE from mainstream mobile platforms.

What is the connection between the browser wars and Node.js?

Ryan Dahl, a software engineer, was tasked with writing high-performance web services in C/C++. For achieving high performance, asynchronous I/O and event-driven design are essential principles. However, writing in C/C++ for such purposes was too cumbersome. So, he began to envision developing web services using a higher-level language. After evaluating several languages, he noticed that many offered both synchronous and asynchronous I/O, but once developers used synchronous I/O, they tended to avoid asynchronous I/O altogether. Ultimately, Ryan turned to JavaScript.

JavaScript’s single-threaded nature inherently prevents synchronous I/O operations, which means it can only use asynchronous I/O due to this "limitation."

With the programming language chosen, a runtime engine was also necessary. Ryan considered writing his own but wisely decided against it. Instead, he opted to use the open-source V8 JavaScript engine developed by Google. He figured he could leverage Google’s investment in optimizing V8 without having to pay for it—an excellent deal.

In 2009, Ryan officially launched the open-source web server project based on JavaScript and the V8 engine, naming it Node.js. Although the name might seem unappealing, Node was the first to bring JavaScript into backend server development. With countless JavaScript developers already in the world, Node quickly gained popularity.

What advantages does JavaScript running on Node.js have over other backend languages?

The most significant advantage is that JavaScript's inherent event-driven mechanism, combined with the high-performance V8 engine, makes writing high-performance web services straightforward.

Additionally, JavaScript is a well-defined functional programming language. In frontend development, developers often write code in a more casual manner, leading to the perception of JavaScript as a "toy language." However, in the Node environment, modular JavaScript code, along with functional programming practices, and the absence of browser compatibility concerns allows for the direct use of the latest ECMAScript 6 standards, fully meeting engineering requirements.

Node.js has loaded