DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Test-Driven Development With The oclif Testing Library: Part One
  • An Introduction to BentoML: A Unified AI Application Framework
  • Build a Full Stack App With SvelteKit and OceanBase
  • Keep Your Application Secrets Secret

Trending

  • 5 Common Security Pitfalls in Serverless Architectures
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Identity in Action
  1. DZone
  2. Coding
  3. JavaScript
  4. NestJS Basics and Core Fundamentals

NestJS Basics and Core Fundamentals

In this post, we look at the basics of NestJS and also the core fundamentals that form the backbone of the framework.

By 
Saurabh Dashora user avatar
Saurabh Dashora
DZone Core CORE ·
Aug. 10, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

A few days back I had the chance to explore NestJS. It is a relatively new framework that makes it easy to develop backend applications using NodeJS. The adoption of the framework is gradually increasing in different organizations.

In this post, we will look at NestJS Basics and Core Fundamentals that form the backbone of this amazing framework. To understand the NestJS Basics better, we will also bootstrap a starter application using Nest CLI.

Let’s get started.

1. The Language

The core language used to write NestJS applications is Typescript. However, since NestJS is built on top of NodeJS, it also supports plain-old Javascript. It uses the latest language features and hence needs something like Babel for compilation purposes.

Due to the flexible nature of NestJS, it is possible to use either Typescript or Javascript depending on the need and expertise of the development team. However, the creators of NestJS seem to lean more towards using Typescript.

2. Setting Up NestJS

NestJS requires NodeJS and NPM as pre-requisites. So, please make sure that both are present on your system before following along.

As per the official docs, NestJS requires NodeJS version above 10.13.

Once NodeJS is installed, it is quite simple to bootstrap a new NestJS project. We first need to install NestJS CLI using the below command.

Shell
 
$ npm i -g @nestjs/cli


Here, we are installing NestJS CLI globally. In case you face some permission-related issues, please refer to this post.

Next, we can use the Nest CLI to create a new project.

Shell
 
$ nest new demo-project


This command will create a directory named demo-project and node_modules (along with some other boilerplate files) will be installed. We will have a /src directory with several core files.

Shell
 
drwxr-xr-x   7 saurabhdashora  staff  224 Jul 24 09:46 .
drwxr-xr-x  16 saurabhdashora  staff  512 Jul 24 09:50 ..
-rw-r--r--   1 saurabhdashora  staff  617 Jul 24 09:46 app.controller.spec.ts
-rw-r--r--   1 saurabhdashora  staff  274 Jul 24 09:46 app.controller.ts
-rw-r--r--   1 saurabhdashora  staff  249 Jul 24 09:46 app.module.ts
-rw-r--r--   1 saurabhdashora  staff  142 Jul 24 09:46 app.service.ts
-rw-r--r--   1 saurabhdashora  staff  208 Jul 24 09:46 main.ts


3. The NestJS Core Files

Below is a brief overview of the core files:

app.controller.ts This is a minimal controller with a single route
app.controller.spec.ts This file contains the unit tests for the controller
app.module.ts The root module for the application
app.service.ts A basic service with a single method
main.ts The entry file of the application that uses the core NestFactory function. Basically, this is responsible for starting up the application instance.

Let’s look at the main.ts in more detail:

Python
 
main.tsimport { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();


Here, in line 1, we import the NestFactory function from the NestJS core package.

The NestFactory exposes a few static methods. Using the create() method, we can create an application instance. To do so, we import AppModule and pass it to create() call. Refer to line 5.

Basically, the application object provides a set of methods that we can use to start our HTTP listener on port 3000. This happens in line 6. We place all of this in the bootstrap() function

Finally, we make a call to the bootstrap() function in line 8.

4. Running the NestJS App

The project scaffolded using the Nest CLI has everything needed to start the application. We can simply start the server by executing the below command:

Shell
 
$ npm run start


We should see an output as below:

Shell
 
> [email protected] start /Users/saurabhdashora/NestProjects/demo-project
> nest start

[Nest] 3640  - 07/24/2021, 10:57:51 AM     LOG [NestFactory] Starting Nest application...
[Nest] 3640  - 07/24/2021, 10:57:51 AM     LOG [InstanceLoader] AppModule dependencies initialized +26ms
[Nest] 3640  - 07/24/2021, 10:57:51 AM     LOG [RoutesResolver] AppController {/}: +6ms
[Nest] 3640  - 07/24/2021, 10:57:51 AM     LOG [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 3640  - 07/24/2021, 10:57:51 AM     LOG [NestApplication] Nest application successfully started +1ms


Basically, this command starts up the HTTP Server listening on the port defined in the main.ts file. In our case, the port is 3000.

Once the application is running, we can visit the browser and hit http://localhost:3000. We will see Hello World! printed.

5. NestJS Platform Independence

NestJS aims to be platform agnostic. This makes it possible for NestJS developers to write reusable logic. In other words, it becomes easy to use the same logic across different types of applications.

Technically, NestJS can work with any Node HTTP framework once an adaptor is created. However, out-of-the-box, it supports both express and fastify. Developers can choose whichever suits their needs.

The platform-express or express is the default choice. It is a minimalist and battle-tested framework with a plethora of community resources.

The other option is platform-fastify. Fastify is a high-performance framework focused on efficiency.

Both the platforms have their own interface. These are seen respectively as NestExpressApplication and NestFastifyApplication.

We can configure them in the app.create() call as below:

XML
 
const app = await NestFactory.create<NestExpressApplication>(AppModule);


The above approach will expose the NestExpressApplication specific methods as part of the app object. However, unless we don’t wish to access a specific method, there is no need to specify a type.

Conclusion

With this, we have successfully looked at NestJS Basics and Core Fundamentals. We installed Nest CLI followed by bootstrapping our first project. Also, we explored the starter files and their purpose along with special attention to the main.ts file that is responsible for starting our application.

Now, we are ready to take the next steps in exploring NestJS further. In the next post, we will look at how to create your first controller in NestJS.

If you have any comments or queries, please feel free to write in the comments section below.

application Node.js unit test Framework Command-line interface shell Database Express NEST (software)

Published at DZone with permission of Saurabh Dashora. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Test-Driven Development With The oclif Testing Library: Part One
  • An Introduction to BentoML: A Unified AI Application Framework
  • Build a Full Stack App With SvelteKit and OceanBase
  • Keep Your Application Secrets Secret

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook