Five Use Cases for Node.js at the Edge

In our last post about Node.js, we looked at five enterprise apps that use the server-side runtime. Many tech leaders, including Uber, Walmart and Netflix, to name a few, have adopted Node.js largely for two reasons: speed and scale. In this post, we will identify five use cases for Node.js at the Edge. With each use case, we will look at what it is, how Node.js is different in its implementation from other JavaScript frameworks, related packages, and real world examples.

Five Best Use Cases

Node.js offers many advantages, including a faster development process, because it allows developers to use JS across the stack, uniting the language and data structures (JSON). It was designed with low latency and streaming in mind, and is an optimal choice for certain types of web applications. These include:

Micro APIs

Node.js can be used to build small, targeted APIs for simple use cases such as search and logging of user-tracking data. Node.js enables simpler implementation for handling micro APIs compared to alternatives like Ruby on Rails, for several reasons, including the ability to:

  • Simply expose your JSON objects with a REST API for the client to consume;
  • Avoid having to do multiple data format conversions, by using a uniform data serialization format across the client, server, and database;
  • Node.js allows you to push the database writes off to the side to deal with later; and
  • The system maintains responsiveness under heavy load.

There are many Node.js API frameworks available offering the ability to extend the capabilities of Node.js and abstract away some of the more complicated aspects of development. Most Node.js frameworks offer fully-featured content exploration, support for customization, flexibility, extensibility, etc.

Express is the most popular Node.js framework, which describes itself as a “fast, un-opinionated, minimalist framework for Node.js.” Its focus is performance and providing exactly what you need. Other options with impressive API generators include Sails, Feathers, and Meteor.

Single Page Application (SPA) Hosting

In Single Page Applications (SPA), the entire application essentially fits onto a single page to provide a desktop app-like experience. SPAs have become a standard approach for making engaging user experiences. Node.js offers the ability to combine dynamic content and code features along with static file delivery of the SPA.

Two examples of major enterprises using Node.js as a tool to build a single page web application are Netflix and Trello. Trello found Node.js to be a good fit since “we knew we wanted instant propagation of updates, which meant that we needed to be able to build a lot of open connections, so an event-driven, non-blocking server seemed like a good choice.” Node.js is able to efficiently handle the asynchronous calls and heavy I/O operations required for building SPAs. This type of app benefits from Node.js’ asynchronous data flow to enable seamless data updates due to the event load as the page receives new data without having to refresh.

Node.js is also a particularly effective fit for SPAs with data-driven single page applications that are using the server to talk to a backend framework (such as GraphQL, Redux or Apollo), while the client-side does all the HTML rendering. Since the server-side and client-side use the same languages, developers don’t need to context switch and are able to use the same language structures and approaches for the full stack, saving time and resources. This leads to better maintainability, as well as faster development time.

Server-side Rendering

Server-side rendering allows an application to render the web page on the server instead of in the browser, sending a fully rendered page to the client whereby the client’s JS bundle takes over, allowing the SPA framework to operate.

By leveraging server-side rendering at the network edge, you can:

  • Deliver faster page load times, providing a better user experience;
  • Improve SEO (low latency) and correctly index web pages;
  • Reduce load on your origin servers;
  • Experience assist with loading the page when the user has a slow Internet connection or an outdated device; and
  • Leverage easier social sharing - SSR provides a featured image and elaborate snippet when your website’s content is shared over social media.

Using Node.js for SSR delivers different advantages to the typical examples of Node.js applications, namely, its library support and browser characteristics as opposed to its concurrency model.

One enterprise example of this type of use is Airbnb, who a couple of years ago, migrated from Ruby on Rails to “a new service that will deliver fully formed, server rendered web pages entirely in Node.js”. The goal was to render almost all the HTML used for the Airbnb product. AirBnB’s developer team found they were able to leverage off-the-shelf components with which they had existing operational experience to address any anomalous behavior thrown up by using SSR with Node.js.

Static Site Hosting

Node.js can be very useful for serving static content and precompiled applications at the edge. Serving static files essentially involves serving your HTML, CSS and JavaScript pages as they are. They are called static because they are not changed by the server nor run, they’re simply sent back as files for your browser to parse through.

Node.js is compatible with many static site generators (SSG), such as Gatsby.js, Next.js, Nuxt.js, and others. A static site generator allows you to use any framework, but the end result will still be simple, lightweight HTML and CSS.

The advantages of static vs. dynamic include:

  • Speed (no database queries to run, no templating and no processing on every request);
  • Version control for content (in a static site, the content is usually stored in flat files and treated as any other component of the codebase as opposed to in a dynamic site where content usually sits in a database elsewhere, separate from the codebase and its version control system);
  • Security (there are fewer opportunities for security vulnerabilities); and
  • Handling unexpected traffic surges (since serving static HTML pages only consumes minimal server resources).

Create Real-Time Applications

When Node.js was built by Ryan Dahl, he was aiming to create real-time websites with push capability inspired by applications like Gmail. So, what was so revolutionary about that back in 2009 when Node.js first came out? After twenty years of stateless web based on the stateless request-response paradigm, Node.js provided the first web application with real-time, two-way connections where both the client and server are able to initiate communication, allowing them to freely exchange data. This was in contrast to the standard web response paradigm, where the client initiates communication. Node.js is also based on the open web stack (HTML, CSS and JS) and runs over the standard port 80.

Examples of real-time applications where Node.js shines include online games, collaboration tools, video conferencing, document sharing and chat, among many others. These types of applications have numerous users performing heavy I/O operations, meaning the risk of overloading the server is high. Node.js allows the collaboration environment to update seamlessly with the help of Event API and WebSockets.

In looking at a specific example, the chat application is a sweet-spot example for Node.js because it is:

  • Lightweight
  • High traffic
  • Data-intensive (although has a low processing/computation rate)
  • Runs across distributed devices

Doing this on alternatives like Ruby on Rails or Django would create too much load on the server because each active client eats up one server process. With Node.js, the server doesn’t need to maintain separate threads for each open connection. It is also simple compared to traditional multi-threaded Java or ROR frameworks; therefore, you can create a browser-based chat application in Node.js that uses minimal resources to serve many clients.

There are many more applications perfect for Node.js, including streaming apps, the Internet of Things (IoT), rewriting body content, and redirecting segments of users to different backends.

With so many developers opting to build on Node.js, more attention is developing around the benefits of hosting Node.js applications at the Edge. Section’s Node.js edge hosting solution is helping developers realize these benefits across the various use cases discussed above, and beyond. If you’ve got a specific Node.js + Edge use case you’d like to explore, we’d love to chat.

Similar Articles