DEV Community

Cover image for Nest.js deep dive series: The simplest Nest application with JavaScript
Ivad Yves HABIMANA
Ivad Yves HABIMANA

Posted on

Nest.js deep dive series: The simplest Nest application with JavaScript

Hi reader!
Welcome to the "Nest.js Deep Dive" series. In this series, we’ll explore the internals of Nest.js to understand how it works and how you can start using it to build your backend applications.

We’ll begin with the complete basics and gradually build strong mental models that will give you confidence when developing your next Nest.js project.

I created this series after doing a lot of research and realizing that there weren’t enough resources that go deep into the framework for people starting. Most tutorials jump straight into practical usage without first building a strong foundation for beginners. This series is the resource I wish I had when I was learning Nest.js myself.



You can find the code discussed in this article HERE
Let’s get started...

What is Nest.js

According to the Nest.js documentation:

“Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications.”

Let’s break down this simple definition to understand what it means:

  • Nest.js as a framework: A framework is a foundational structure you can build software on, so you’re not starting entirely from scratch. Since Nest.js is a framework, it “forces” its users to structure their code in a predefined way, making it easier to maintain, test, and reason about.
    .

  • Server-side application: Nest.js is built with Node.js to create applications that run on the server.

Let's talk a bit more about building a server-side application

The term "server" can mean different things depending on context. Sometimes it refers to a "physical computer" that provides resources to other computers (clients). Other times, it refers to the "software application" running on that machine for handling requests and sending back responses.

For example, when you open a profile page on Facebook, your browser sends a request to Facebook’s servers (the machines). On those servers, a "web server application" processes the request and responds with the page you see.

Nest.js and Express.js

Another popular framework for building web servers in Node.js is Express.js. In fact, under the hood, Nest.js uses Express (or similar frameworks) to simplify the process of creating server-side applications.

The main difference between the two is that Nest.js is opinionated, while Express is unopinionated. This means the creators of Nest.js have strong "opinions" and a set of best practices for how backend code should be organized and written. With Express, however, you are free to define your own structure and approach.

Enough with the words, let's start writing some code...

Creating the simplest Nest.js application

Usually, to create a Nest.js application, we use the Nest CLI. The CLI sets up and configures most of the boilerplate for you, giving you a working application in seconds.

The downside is that it works like magic; we don’t get to see what’s happening under the hood. In this article, we’ll create the base files ourselves. For simplicity, we’ll start by using Nest.js in a JavaScript environment.

Note: Nest.js uses and recommends TypeScript. We are only using JavaScript here for learning purposes. Later in the series, we’ll discuss using TypeScript and the CLI to simplify development once we understand the basics.

Set up a new Node.js application

npm init -y
Enter fullscreen mode Exit fullscreen mode

Installing the required dependencies:

npm i --save @nestjs/core @nestjs/common @nestjs/platform-express rxjs reflect-metadata
Enter fullscreen mode Exit fullscreen mode

Understanding the packages we just installed

  • @nestjs/core: This is the core package that includes essential functionalities and the environment needed to run a Nest application.

  • @nestjs/common: This contains elements commonly used across a Nest application, such as controller decorators and HTTP handling logic (we’ll cover this in detail later).

  • @nestjs/platform-express: Since Nest is built on top of Express, this package enables seamless integration between Nest and Express.

  • rxjs: Nest uses rxjs internally to handle asynchronous tasks and allow reactive programming, such as processing incoming requests or managing data flows.

  • reflect-metadata: This package allows Nest to attach additional metadata to classes and functions, which is required for decorators (we’ll explain decorators later in the series).

Initializing the Nest application

Create an index.js file and add the following piece of code

import { NestFactory } from "@nestjs/core";

const bootstrapp = async () => {
  const app = await NestFactory.create(); // creating the app
  await app.listen(3000); // listening on Port 3000
};

bootstrapp();
Enter fullscreen mode Exit fullscreen mode

Running the code above with node index.js will cause an error because NestFactory.create() expects arguments, which we haven’t passed yet.

Image showing error thrown by nest application

Even though the code will throw an error, let’s take a moment to understand what’s happening here:

  • We are importing the NestFactory object from the @nestjs/core package. As the name “factory” suggests, this object contains methods required to create a Nest application.
    .

  • We define a function called bootstrap that initializes (or "bootstraps") a new Nest application by calling NestFactory.create(). This method returns a promise that resolves to the app object, so we await it to get the actual application instance.
    .

  • From the app object, we call the listen method and tell it to listen on port 3000. This means any requests sent to port 3000 will be handled by our Nest application.

Now let’s fix our NestFactory.create() error

The NestFactory.create() method expects at least one argument, which is the root module of our application. For now, don’t worry too much about what a module is. At a high level, a module is simply a class that holds information about the available controllers, services (we’ll talk about these later), and other application logic. You can think of it as the "root of our application logic".

To keep things simple, we can just create a basic plain JavaScript class with nothing inside it for now, just to make NestFactory.create() stop crying 😂

Update the code in index.js to be the following

import { NestFactory } from "@nestjs/core";

class AppModule {} // Just an empty class for now

const bootstrapp = async () => {
  const app = await NestFactory.create(AppModule); // we create the app with our module
  await app.listen(3000);
};

bootstrapp();
Enter fullscreen mode Exit fullscreen mode

That’s it, that’s the simplest Nest application you can create in JavaScript :). If you try to open http://localhost:3000 in the browser or something like Postman, you should get an error that looks like

{
    "message": "Cannot GET /",
    "error": "Not Found",
    "statusCode": 404
}
Enter fullscreen mode Exit fullscreen mode

This is because we have not implemented any endpoint, but at least our web server application is up and running and responding to requests.

In the next article we will, we will build on our current progress go deep into Nest.js Module

See you next...

Top comments (0)