NestJS vs. Express.js
Tags: webdev
Published: 2022-06-28 16:06:59

**Written by [Temitope Oyedele](https://blog.logrocket.com/author/temitopeoyedele/)✏️**

When we want to build a large-scale application with a framework, Express.js is usually the first thing that comes to mind. Express has gained popularity in the last few years among developers. NestJS is another popular framework that can do the job pretty well, too. 

As both Nest and Express are used as frameworks for Node.js applications, there’s been a hot debate over which option is better for developers starting a new Node project. In this article, we’ll be comparing NestJS and Express.js to help developers make an informed decision. What we’ll cover:

*   [What is NestJS?](#what-nest-js)
    *   [NestJS core components](#nest-js-core-components)
    *   [How to install NestJS](#how-install-nest-js)
    *   [Features of NestJS](#features-nest-js)
*   [What is Express.js?](#what-express-js)
    *   [How to install Express.js](#how-install-express-js)
    *   [Features of Express.js](#features-express-js)
*   [NestJS vs. Express.js](#nest-js-vs-express-js)
    *   [NestJS vs. Express.js: Opinionated and un-opinionated](#nest-js-vs-express-js-opinionated-un-opinionated)
    *   [NestJS vs. Express.js: Popularity](#nest-js-vs-express-js-popularity)
    *   [NestJS vs. Express.js: Performance](#nest-js-vs-express-js-performance)
    *   [NestJS vs. Express.js: Architecture](#nest-js-vs-express-js-architecture)
    *   [NestJS vs. Express.js: Unit testing](#nest-js-vs-express-js-unit-testing)
    *   [NestJS vs. Express.js: Use cases](#nest-js-vs-express-js-use-cases)

## What is NestJS? <a name="what-nest-js">

NestJS is a Node.js framework for building server-side applications. It is based on TypeScript and JavaScript. 

This framework is inspired by Angular, so most of what you find in Angular can also be found in Nest, including providers, middleware, components, and services. It is safe to say that Nest can be easily learned by Angular developers for any type of project. 

Nest makes use the Express HTTP framework by default. However, Nest is platform agnostic, meaning it can work with any Node HTTP framework. For example, it can optionally be [configured to use Fastify](https://www.npmjs.com/package/@nestjs/platform-fastify), which can improve the Node framework. 

One of the cool things about NestJS is that anything supported in Express (i.e., Express functions) is also supported in Nest.

### NestJS core components <a name="nest-js-core-components">

Let’s go over some of the core components of NestJS. 

A module is a class that has been annotated with the `@Module()` decorator. Nest uses the metadata provided by the `@Module()` decorator to organize the application structure. 

Each NestJS application contains at least one module, known as the root module. Nest uses the root module as a starting point to resolve the structure and relationships of the application. 

Dynamic modules are a powerful feature of the Nest module system. This feature allows you to easily create customizable modules that can dynamically register and configure providers. 

Providers are very important, as Nest relies heavily on them to create relationships between different objects. A provider can be injected as a dependency. 

Classes like services, repositories, and helpers can be treated as providers; simply adding the `@Injectable()` decorator from Nest will handle the resolution, making dependency management extremely simple. 

When an HTTP request is received, the routing mechanism routes it to the correct controller within NestJS. Controllers handle incoming requests and respond to the application's client side.

### How to install NestJS <a name="how-install-nest-js">

Nest includes an excellent CLI that makes it simple to scaffold a Nest application. In your terminal or command prompt, type the following:

```bash
npm i -g @nestjs/cli
```

Let's use the `cd` command to change into the directory where we want to build our app. Run the following commands:

```bash
nest new nest-blog-api
cd nest-blog-api
npm run start:dev
```

Go to http://localhost:3000 in any browser. You should see a “Hello World” message.

### Features of NestJS <a name="features-nest-js">

Let’s explore some of the notable features that set NestJS apart. 

Working with Node and Express is great for building a small, lightweight app where the code is nice and easy to read. However, as things start to get complex and you add more features to your application, your code will start to get a little messier and harder to manage. 

This is where NestJS comes in. Nest can organize your application into self-contained modules, each with its own responsibility. A module can contain related controllers and services and keep them fairly isolated from the rest of your application. 

[Nest also supports dependency injection](https://blog.logrocket.com/top-five-typescript-dependency-injection-containers/). Using dependency injection means you don’t need to have a hard dependency on things like components, services, and middleware within your code. 

Exception filters are another cool Nest feature. All applications encounter exceptions from time to time. How you handle exceptions is really important, and conveniently, Nest sorts all of that out for you. 

Nest includes an exceptions layer that is responsible for handling all unhandled exceptions across an application. When an exception is not handled by your application code, it is caught by this layer, which sends an appropriate user-friendly response automatically. 

You also get easy MongoDB support with Nest. A lot of web apps built with Node use [the MEAN stack, which consists of MongoDB, Express, Angular, and Node](https://www.mongodb.com/mean-stack). One of the most popular libraries for accessing the Mongo database is Mongoose. 

You can easily connect to the MongoDB database and use it to build scalable applications using the [NestJS Mongoose package](https://www.npmjs.com/package/@nestjs/mongoose). 

Finally, as we already mentioned, NestJS uses the Express framework by default as the request processing pipeline. This means if you are already familiar with Express processing, you’ll be able to adapt your Express middleware to use within Nest.

## What is Express.js? <a name="what-express-js">

Express is a Node.js web application framework that provides a wide range of functionality for constructing web and mobile applications. It is a layer built on top of Node that aids in the management of servers and routes. 

You can use Express with Node to create single-page, multi-page, or hybrid web applications. It supports the [MVC architectural pattern for designing web applications](https://blog.logrocket.com/building-structuring-node-js-mvc-application/): Model, View, and Controller.

### How to install Express.js <a name="how-install-express-js">

To use Express for our project, we first to install it:

```bash
npm install express
```

Then we want to import Express inside our project:

```bash
import express from 'express';
```

Next, we need to initialize our app, give it a port to listen to, and respond to a request on that path:

```javascript
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () =>
  console.log('Example app listening on port 3000!'),
);
```

### Features of Express.js <a name="features-express-js">

Let’s go over some of the features of Express. 

Middleware is a program component that has access to a database, client requests, and other middleware. It is primarily responsible for the systematic organization of various Express.js functions. 

When it comes to routing, Express includes a sophisticated routing mechanism that uses URLs to preserve the state of the webpage. 

Finally, Express includes template engines that enable developers to create dynamic content for web pages by creating HTML templates on the server side.

## NestJS vs Express.js <a name="nest-js-vs-express-js">

In this section, we will directly compare various aspects of Nest and Express, including example use cases for each.

### NestJS vs. Express.js: Opinionated and un-opinionated <a name="nest-js-vs-express-js-opinionated-un-opinionated">

Nest is a framework with strong opinions. It adheres to the [design paradigm of "convention over configuration,"](https://en.wikipedia.org/wiki/Convention_over_configuration) which allows developers to use standard tools and code in a specific manner, thus reducing the need for explicit configuration. 

In addition, NestJS is [Angular-based, so TypeScript is its primary programming language](https://blog.logrocket.com/angular-11-what-to-expect/), although you can also use JavaScript. The use of TypeScript ensures that the application is reliable and bug-free. 

Express.js is a framework without strong opinions — in other words, un-opinionated. This means it doesn't have a set of pre-defined rules to follow. Developers often use this opportunity to experiment with different scenarios and write code as needed.

### NestJS vs. Express.js: Popularity <a name="nest-js-vs-express-js-popularity">

When it comes to which framework is the most popular, Express.js takes home the title. With over [57,000 stars on Github](https://github.com/expressjs/express), Express is the most popular framework. It brands itself as a fast, un-opinionated, and minimalist framework. 

Nest.js follows closely behind. It has over [47,000 stars on Github](https://github.com/nestjs/nest), ranking second among the [top Node.js frameworks by Github stars](https://github.com/VanoDevium/node-framework-stars).

### NestJS vs. Express.js: Perfomance <a name="nest-js-vs-express-js-performance">

Express can execute multiple operations independently of each other using asynchronous programming. Nest employs the Express framework by default. 

However, Nest also provides an alternative way to change the underlying framework from Express to Fastify for significantly improved performance.

### NestJS vs. Express.js: Architecture <a name="nest-js-vs-express-js-architecture">

Nest offers a ready-to-use application architecture using controllers, providers, and modules. This enables developers and teams create applications that are simple to test and maintain. 

[Express does not require a specific structure](https://blog.logrocket.com/organizing-express-js-project-structure-better-productivity/), which can provide flexibility for small or one-person development teams. However, this can become a problem as team size or app complexity grows, especially with developers working on different aspects of the app.

### NestJS vs. Express.js: Unit testing <a name="nest-js-vs-express-js-unit-testing">

The Nest CLI comes with a [Jest-based default testing environment](https://blog.logrocket.com/unit-testing-nestjs-applications-with-jest/). When a service, interceptor, or controller is created, the CLI creates a spec file. This file includes auto-generated testing bed code, eliminating the need for developers to write additional code for unit testing. 

In Express, unit testing necessitates the construction of distinct codes, which takes time and might slow down the application's productivity.

### NestJS vs. Express.js: Use cases <a name="nest-js-vs-express-js-use-cases">

Let’s take a look at some use cases for Nest vs. Express. 

Both are suitable choices for building single-page applications (SPAs), a modern application development strategy in which the entire app is routed through a single index page. 

The serve-static module in NestJS allows you to create SPAs as fast as possible. Express.js is also a great framework for creating an API that connects these SPAs and serves data regularly. 

Some other examples of what to build with NestJS include enterprise-level web applications and ecommerce applications. 

NestJS applications scale well, making them perfect for large, enterprise-level applications. It's no surprise that [leading companies like Adidas, Roche, Trilon, and others use Nest](https://docs.nestjs.com/discover/companies). 

We can also easily [construct ecommerce websites by combining NestJS](https://blog.logrocket.com/how-build-ecommerce-app-nestjs/) with a frontend framework like React, Angular, or Vue. 

Other examples of what you can build with Express.js include fintech applications and streaming applications. 

A computer software or other technology used to support or facilitate banking and financial services is referred to as fintech. More and more financial institutions are creating fintech applications, and Express is the framework of choice for creating highly scalable finance applications. 

Real-time streaming services are complex, with multiple levels of data streams. To create such an app, you'll need a robust framework such as Express.js that can efficiently handle asynchronous data streams.

## Conclusion

According to some developers, you are ahead of the game if you are already using NestJS. This framework gives you a significant advantage early on and also helps you [take your Node backend to the next level](https://blog.logrocket.com/node-back-end-next-level-nestjs/) by properly structuring your app. 

However, Express.js is one of the best and most popular backend development frameworks using JavaScript, and will likely remain so for some time. Which option do you prefer? Share your thoughts in the comment section!

---

##200’s only :heavy_check_mark: 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.](https://logrocket.com/signup/)

[![LogRocket Sign Up](https://blog.logrocket.com/wp-content/uploads/2019/12/network-request-filter-2-1.png)](https://logrocket.com/signup/)

[LogRocket](https://logrocket.com/signup/) is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. 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.](https://logrocket.com/signup/)