Comparing top Node.js frameworks for frontend developers
Matt Angelosanto
Posted on November 1, 2021
Written by Hulya Karakaya ✏️
The Node.js runtime allows developers to build both front and backend applications using only JavaScript. This means no more context switching between different languages like PHP for the backend and JavaScript for the frontend. Once seen as a toy language for amateurs, Node has revolutionized the web.
Node also helps frontend developers because, if you were a developer before Node was introduced, you had to learn a completely different language to write server side code like PHP. Thanks to Node and its huge ecosystem, frontend developers are able to work with the backend with JavaScript as well.
With Node, you can create production-ready applications ranging from real time chat applications, and microservices, to command line tools, web servers, and APIs. Node handles a high number of concurrent requests thanks to its event-driven model. Many companies embrace Node in production like Netflix, Uber, Nasa, Paypal, and Walmart.
In this article, we will compare Node frameworks according to their usage, features, disadvantages, downloads, and other metrics.
Why use a framework?
Before diving into the different frameworks, we will look at why we need them to create web applications.
Writing server side logic is complex, and coding an entire app from scratch is time consuming. As developers, we need to focus on the business logic rather than wasting time creating an app from scratch.
Frameworks handle the heavy lifting and allow us to build applications more quickly. The framework provides a set of helper functions, a suite of tools, and rules. It helps us structure our application, and write clean code in a short period of time.
Many web frameworks have abstracted web protocols into higher level APIs and provide a richer interface for building web frameworks. These frameworks usually grab the underlying built-in power of Node and give us a simpler API to create more complicated features in our web servers. Of course, frameworks add abstraction to our code, but if you are building a big web application, they come in handy.
Every framework tries to solve a problem and provides its own solution. The solutions and philosophies of each framework will differ from each other. However, Node frameworks share some commonalities, like mapping URLs to content aka routing and templating for dynamic content.
Types of Node.js frameworks
You may feel excited to learn a new framework, but the sheer amount of frameworks may overwhelm you. That's why I will start off with a diagram, here we can at least know which framework might be the best choice for your project.
There are multiple ways to group frameworks, and some of them can belong to multiple categories. For example, I have included NestJS under Rest API, but you can also use it as a Full-stack and MVC framework, or you can just spin up a REST API server.
HTTP server frameworks
These frameworks are based on Express.js and help you build apps based around HTTP verbs and routes.
MVC frameworks
These frameworks offer MVC architecture. MVC stands for Model, View, and Controller, a software architecture design pattern that splits application functionality into three parts.
Models handle data logic and represent how data is stored. Views handle presenting the information to the user, and controllers handle request flows and delegate information between the model and the view. The controller will get the data information from the model and then pass that data into the view.
Sails.js, Strapi, and Adonis support MVC architecture. You can use it if you don’t want to spend time structuring your apps. In addition to that, having MVC architecture makes it easy for other developers to understand your code.
Full-stack MVC frameworks
Meteor and Feathers are some of the full-stack Node frameworks. They use JavaScript on the client and server side. This is also known as an isomorphic code.
These frameworks might come with many features, including templating engines, WebSocket libraries, and ORMs.
Rest API frameworks
NestJS, Loopback, and Restify are REST API frameworks. You can use them to spin up a fast Node server, or just to handle CRUD requests.
Some statistics
Okay, so what? Which one should you choose? This diagram will give you an idea of how popular the framework is, how many contributors they have, monthly downloads on npm, and the age of the framework.
As we can see, most of the frameworks are pretty old, and the leading framework with the most downloads on npm is Express.
Following Express, we have Socket.io, Koa.js, and NestJS. As for their popularity on GitHub, Express and Socket.io are again leaders with Meteor, NestJS, and Strapi following them.
Now, we will look at the five most popular frameworks and compare their features, selling points, and which companies are using them. We will compare Express, Koa, Socket.io, NestJS, and Fastify.
Hapi.js is older and has more npm downloads than Fastify, but Fastify is growing really fast, and there are not many resources on the Internet about Fastify, which is why I thought it should be featured in this article.
Express.js
Express.js is undeniably the king of Node frameworks with 74.22m monthly downloads on npm. It is considered the de facto web server framework for Node.
Express was released in 2010, and written in JavaScript. It's inspired by the Sinatra framework because of Ruby's efficiency and maintainability.
The reason developers love Express is that it's lightweight, fast, minimalist, and unopinionated. It's designed to build and run web apps quickly.
Features and advantages of using Express.js
Express has features (like functions) that speed up the process by writing only a few lines of code. Express is only a thin layer to Node, and because of Node’s asynchronous nature, we can run multiple requests concurrently. This means Express provides high performance and a fast development experience.
Express’ robust and resilient routing system handles requests through routes so that you may write responses to specific endpoints.
Express doesn’t give a universal way of organizing things. There are no strict rules, so you can decide how to structure your app or which middleware to use. For example, you are free to choose any design pattern (MVC, MVP, MVVM, etc.), however, the most common is the MVC pattern.
It is easy to integrate third-party services and middleware with Express. You can have middleware for parsing requests, cookie headers, sessions, security headers, or libraries. This allows using additional libraries for authentication, database access, session management, and much more. You can easily integrate with SQL or NoSQL databases.
The Express system supports 20+ template engines to simplify generating HTML. Some popular ones are Pug, Mustache, and EJS.
Express has a sizable community, and it doesn't seem like it's going anywhere soon. Its documentation is also very detailed. Plus, Express has a shallow learning curve. If you are familiar with JavaScript, it's easy to pick it up. In fact, most frameworks are built on to off Express.
Express is also easy to test because of its middleware and routes that modularize projects.
Disadvantages of using Express.js
- Express requires a lot of labor intensive tasks like setting up endpoints and middleware
- As I said before, it's unopinionated, so you need to configure libraries and features yourself
- While writing Express code, we might run into callback hell or pyramid of doom. You can solve this problem by using Promises
Who uses Express.js?
Express is used by the following companies:
- Mulesoft
- IBM
- Yandex
- Uber
- Accenture
When to use Express.js
Express is flexible, and you can build any kind of single-page, multi-page, or hybrid web and mobile application, from small to full production. You can start by building simple static file servers to JSON APIs.
Koa.js
Koa.js is the modern version of Express. It is lighter, more expressive, and more robust.
You can use Koa to create web services just like Express. In fact, this framework was designed by the team behind Express, and is referred to as the future of Express. The team reduced the middleware bundle in Express, improved it with modern JavaScript syntax, and gave more freedom to developers.
Differences between Express.js and Koa.js
If we compare Koa and Express, we can see that Koa has removed most of the features found in Express. Koa is very unopinionated, and it doesn't ship routing, templating engine, or JSONP out of the box. The main purpose of this is to make Koa faster. It gives developers more freedom, and fewer complexities while writing code.
Koa also has more options for customization. So, if the developer wants to work with middleware, he or she can create it themselves or use the already built-in middleware.
Koa provides add-on modules like Koa router, Koa EJS templating, Koa Body Parser, and much more.
Features of Koa.js
Koa focuses more on modern features of the JavaScript (ES6) language like generators, async functions, and the Node runtime. It uses Promise-based flows, and async-await syntax to get rid of callbacks, making the code more manageable, cleaner, and more readable.
Instead of callbacks, Koa leverages ES2015 generator syntax to define middleware, which you can use the yield
keyword to exit and then reenter.
Koa uses a context
object, which encapsulates request and response (req/res
) objects into one. This helps developers build APIs more efficiently by using several helpful methods.
Finally, Koa uses cascading middleware. Middleware will run in a cascading fashion until the last middleware is reached thanks to asynchronous functions.
Advantages of using Koa.js
- Koa is easy to learn if you know Express
- It has only 600 lines of code. This helps developers write thinner middleware
- Koa has a better performance than Express for handling more requests per second
- Koa has better control over error handling with try/catch, and it’s more modular
- Refactoring larger code bases in Koa is easier due to modularization
- Koa provides a minimal interface for developing apps and APIs
Disadvantages of using Koa.js
- Koa has a relatively small community
- It is not compatible with Express-style middleware
- Generators make it harder to migrate to other Node frameworks in the future. It creates tight coupling with the framework
- Koa uses ES6 generators which are not compatible with middleware from other frameworks
- Koa doesn't have a built-in middleware, and conventional middleware such as req, res, and next don’t work with Koa
Who uses Koa.js?
- Paralect
- GAPO
- Shimo Website
When to use Koa.js
Koa is great for building scalable, lightweight web apps, HTTP APIs, and single page web apps.
Socket.io
Socket.io is mainly used for building real time chat applications like WhatsApp and Facebook Messenger. It establishes a bidirectional communication between the web client and servers.
To understand this better, we need to understand how clients and servers normally communicate over HTTP. The client makes a request, and the server responds to the client’s request. But this is a one way communication; the server cannot start communication with the client. This is for various reasons. The HTTP protocol is stateless, and it is hard for the server to find the IP addresses of all the different clients that are accessing.
In some situations, the server needs to update the client in real time. For example, if we have a chat application, the server should broadcast new messages instantly to all users. Instead of using an HTTP connection, we need to use WebSockets. WebSockets allow real time, two way communication between the frontend and backend.
Socket.io has two parts, a client side library that runs on the browser, and a server side package for Node. Both components have an API that's nearly identical because we can run code both in browser and server. It follows EventEmitter
model of Node.
So, for WebSockets to work, the client opens a persistent connection to the server, and both parties can send messages back and forth to each other.
Features of Socket.io
Socket.io provides a reliable WebSocket connection for Node applications. It provides real time analytics in fewer lines of code with high speed support.
If the server and client don’t agree on a handshake, Socket.io falls back to long polling and supports alternative protocols (WebSockets, Flash, XHR, and JSONP). It can support a wide range of browsers.
Socket.io also features binary and multiplexing support; we can send any blob data like images, audio, or video from the client or server side.
The framework offers auto reconnection support. If a client is disconnected, it will keep retrying unless stopped manually.
Finally, Socket.io features automatic error detection and correction, and forges deep connections even in the presence of proxies and private firewalls.
Advantages of using Socket.io
- Socket.io has built in support for adding middleware
- It’s easy to get started, and with it, one can develop applications quickly
- It provides a more straightforward way of setting up audio and video streaming functionalities
- Large community
Disadvantages of using Socket.io
- It uses callbacks
- It doesn’t provide a message guarantee to acknowledge that it received a message, so we need to write custom logic in our application to handle such scenarios
Who uses Socket.io?
- Patreon
- Alibaba Travels
- Plaid
- Trello
- Bepro
- Barogo
When to use Socket.io
Socket.io is useful for video conferencing apps, interactive streaming media, multiplayer games, and dynamically updated social network sites.
Fastify
Fastify is inspired by Hapi and Express. It claims to be the fastest web framework around, and, according to their benchmarks, it’s twice as fast as Express. The only case where Fastify falls behind Express is in handling empty requests.
According to the 2020 State of JS survey, Next and Express are tied in as the most used backend framework (92 percent), followed by Fastify (89 percent).
This is because Fastify focuses on minimizing performance overhead with a powerful plugin architecture, and keeps the developer experience in mind.
Features of Fastify
Fastify automatically parses JSON with efficient rendering, and provides quick routing. Therefore, it is highly performant, and can serve up to 30,000 requests per second.
It is extensible thanks to its plugins, hooks, and decorators. It has an ecosystem of 200 plugins.
Fastify also works well with Typescript, supports AWS Lambda, has a GraphQL adapter, and comes with a lightweight logger.
Finally, Fastify shuts down an application gracefully. It stops accepting new connections and works to close all outstanding “keep alive” connections before exiting the process.
Advantages of using Fastify
- Zero overhead in production
- Good developer experience
- Automatic security and data validation
- Easily testable
- Semantic versioning, long-term support
- Cleaner syntax for writing async code in controllers
Disadvantages of using Fastify
- Doesn’t have a huge community, and if you’re stuck with a bug, you may need to fix it yourself
- Fastify has a shared ownership principle, which means you can create a PR and contribute to the community
Who uses Fastify?
- Microsoft
- Hotstar
- Future Foundry
When to use Fastify
While Fastify can be used to create web applications, it excels when you're building JSON-based APIs. It works great for small and big projects alike; it scales well. You can easily migrate to microservices (or even serverless) and back.
NestJS
NestJS is a progressive, flexible, and versatile REST API framework for building efficient, reliable, and scalable server side applications.
Nest uses modern JavaScript, and is built with and supports TypeScript out of the box. It combines the elements of OOP (object-oriented programming), FP (functional programming), and FRP (functional reactive programming).
It builds upon Express internally, but you can also configure it with Fastify.
Nest is inspired by Angular, it uses the concepts of dependency injection, modularity, and decorators. You can build MVC (model view controller) apps, REST, and GraphQL APIs. Nest provides a built in wrapper around Apollo GraphQL.
Features of NestJS
Nest provides a powerful Command Line Interface (CLI) to boost productivity and ease the development process.
It also supports REST and GraphQL APIs out of the box, and you might use it to build a full stack application.
Nest provides an option to build microservice-based applications out of the box. When you spin up a Nest server, it comes with a clear architecture based on a few simple components (controllers, modules, and providers). This makes it easy to split applications into microservices.
Finally, Nest has easy unit-testing applications. It has dedicated setups for all kinds of application testing.
Advantages of using NestJS
- Nest can be scalable thanks to the flexibility of JavaScript and the robustness of TypeScript
- Detailed and well-maintained documentation
- It has active codebase development and maintenance
- It is open source (MIT license)
- Code generation helps to develop applications faster
- Has a quickly growing community
- Follows strict design principles that do a lot of basic development activities for developers
Disadvantages of using NestJS
- It has Angular concepts, so for developers who don't know Angular, it may be hard to grasp at first, even if you don’t need to know Angular before working with Nest
- Steep learning curve
- The community is small when compared with Express
Who uses NestJS?
- Adidas
- Autodesk
- Neoteric
- Sanofi
When to use NestJS
Nest is great for building complex, large-scale enterprise applications.
Choosing the right framework
We have covered quite a lot in this article, starting off with Node, why we need frameworks, types of frameworks, and statistics. After that, we went over the five most popular frameworks, namely Express, Koa, Socket.io, Fastify, and Nest. This article’s purpose is to give you an idea of how frameworks differ from each other and help you choose a Node framework based on your specific needs.
What do you think of these frameworks? Did I miss your favorite? Let me know in the comments below.
200’s only ✔️ Monitor failed and slow network requests in production
Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third party services are successful, try LogRocket.
LogRocket is like a DVR for web apps, recording literally everything that happens on your site. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.
LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. Start monitoring for free.
Posted on November 1, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.