Node.js is a server-side environment that allows Node developers to build servers and network applications with JavaScript for the first time. This means entire sites can be run on a unified JavaScript stack—both the client-side software, and the server-side software.
Technically, it’s a development platform, not a framework—with numerous frameworks that run on top of it. It’s also a runtime environment, a component of a development platform that allows developers to test a software program while it’s running—simulating how the program will behave once it’s been executed, so any bugs or errors can be tracked and fixed. Node.js runs a programming construct called an “event loop,” which waits for client requests then sends them to the server or database. It’s best to think of Node.js as less of a full-fledged server, and more of a foundation upon which you can easily add libraries and workhorse connectors like HTTP, SSL, and TCP to quickly set up a working dynamic web server—all with only a few lines of JavaScript.
- It’s the N in the MEAN software stack. Software stacks are bundles of all the necessary components needed to build sites that are proven to work well together, so they’re dubbed “a stack.” They include: an OS, a database, a server, and a programming language. The MEAN software stack is a JavaScript-powered stack: MongoDB, Express.js, AngularJS, and Node.js.
- It’s open source and cross-platform.
- It’s an asynchronous, non-blocking, event-driven I/O system. In other words, your application doesn’t have to wait around for an initial operation to complete before it can respond to another event. It can instead queue callbacks in the event loop in the order it receives them, and perform other processes before the initial transmission has finished. Because it brings JavaScript’s asynchronous method to back-end programming, it takes calls separately from one another, on an individual basis, which makes it excellent with handling cascading calls made through multiple APIs—things that could slow down traditional back-end programming with languages like PHP.
- It’s built on Chrome’s JavaScript virtual machine, V8. V8 is Google’s JavaScript engine and is responsible for executing Node’s code.
WHAT IS NODE.JS USED TO BUILD?
JavaScript programmers can use Node.js to build real-time web APIs, network programs (like servers), and real-time, moderately data-intensive applications. It builds fast server applications in an event-driven model that achieves concurrency without the hassle of managing application state across multiple threads (i.e., avoid deadlocking processes).
On the other hand, it’s important to remember that the single-thread, multi-process approach can run into a different bottleneck at higher per request latencies (greater than 2,000 simultaneous requests a second)—the speed difference between IPC (interprocess communication) used by Node.js and communication between multiple threads becomes more noticeable. For truly data-intensive, real-time applications, you may want to choose a database system based on a language that better optimizes threading, like Java.
The popularity, ease-of-use, and speed of Node.js is perfect for most general web server applications, especially in situations where the speed with which you can add a new feature trumps the need to squeeze out additional performance from an application that is already fast enough to meet your needs. This leads to fast, efficient server programs that aren’t slowed down by traditional bottlenecks.
Added bonus: Web servers or network servers built by Node.js are written in JavaScript, making them naturally compatible with browser-based JavaScript. As a result, everything works in harmony.
THE EXPRESS.JS FRAMEWORK & THE KOA.JS FRAMEWORK
Node.js builds server apps with a lightweight, efficient JavaScript framework called Express.js. There are other frameworks it’s compatible with, but Express.js is best known as another quarter of the MEAN software stack. Newer to the scene and following in Express’ footsteps is the Koa.js framework, a callback-less lightweight framework written by the same author as Express. Get a rundown of more Node frameworks in this 15 Frameworks to Know for Next-Level Node Development article.
SO, WHAT MAKES NODE.JS SO EXCELLENT (AND EFFICIENT) FOR BACK-END PROGRAMMING?
Two words—programmer productivity. While other server-side technologies have their own means of creating dynamic, asynchronous web servers, Node.js is simply easier to use. At a glance, it’s similar to back-end platforms powered by PHP, Java, and Python, but Node.js solves for speed issues in a few key ways that result in less programming overhead for the developer. A few sites that use Node.js include: PayPal, LinkedIn, Groupon, Walmart, Yahoo!, Intuit, and Voxer. Here’s a quick look at six of its advantages:
- What makes JavaScript fast makes Node.js fast. That’s a broad way of looking at things, but essentially, Node.js is harnessing the speed and power of JavaScript. And because JavaScript is everywhere on the front end, migrating it over to the back end means a more fluid stack right off the bat. Node.js servers work in lockstep with JavaScript, have excellent JavaScript libraries built in, and make life easy for developers who can now work on both front- and back-end programming without having to shift gears. Everything that makes JavaScript fast is driving Node.js—and it all comes down to browser compatibility, the use of JSON, and the event-loop model of handling requests.
- It’s cross-platform. JavaScript works on nearly every OS, because it was born in the browser and had to be compatible. It’s lent this flexibility to Node.js. It runs on OS X, Windows, Linux, FreeBSD, and more.
- Event-driven programming. Node.js has made event-driven programming possible on the server-side—bringing vast improvements to server speed. The event loop paradigm synergizes perfectly with the way web browsers work. In the same way that JavaScript is designed to internally handle an event loop, Node.js can snap requests onto the event loop and process them, effectively achieving concurrency without having to assign additional threads. It’s similar to (but more straightforward than) Ruby’s EventMachine or Python’s Twisted event-processing engines—eliminating concurrency issues that sometimes slow down server-side applications.And, because Node.js works asynchronously, similar to AJAX, it handles server connections on an event-driven basis only. Calls are addressed individually, at the same time, but separate from one another—and Node.js will go to sleep in the absence of events. That means less waiting, bottlenecks, and no overloads.
- The V8 virtual machine. The engine powering Node.js is Google’s Chrome JavaScript runtime, the V8 virtual machine. It’s incredibly fast and compiles JavaScript code into “machine code”—a language readable by whatever hardware or operating system it’s being run on. Used by companies like Walmart to build APIs, and written in C++, the V8 virtual machine is the main reason it can boast so much speed.
- JSON has done for Node.js what it did for JavaScript. JSON is an incredibly simple, fast text format used to describe and transfer data. JSON and JavaScript go hand in hand, and have contributed to the strength of one another—and Node.js capitalizes on that. JSON also led to the rise of document-oriented NoSQL databases like MongoDB—one fourth of the Node.js-powered MEAN software stack.
- Like AJAX, Node.js only wakes up for a “callback.” This is a whole different approach, now applied to server-side applications. The parallel connections mentioned above trigger what’s known as a “callback”—between callbacks, Node.js goes into hibernation. This event model is borrowing from JavaScript’s browser behavior in this way—there’s no threading, and no waiting for a loop to close to initiate a new connection.
0 comments:
Post a Comment