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.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
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.
$ 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.
$ 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.
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:
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:
$ npm run start
We should see an output as below:
> demo-project@0.0.1 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:
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.
Published at DZone with permission of Saurabh Dashora. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments