Hello and welcome to NestJS!
In this course, we'll get familiar with the essentials of NestJS by building a basic CRUD application. In this particular unit, we'll acquaint ourselves with the basic structure of a NestJS application and the CodeSignal environment.
Before we dive into the code, let’s take a moment to understand the technologies we’ll be using in this unit:
- TypeScript: NestJS is built on top of TypeScript, a typed superset of JavaScript that provides static typing and other advanced features for writing maintainable code.
- NestJS: A framework for building scalable server-side applications, heavily inspired by Angular.
- Express: NestJS uses Express as the default HTTP server, though it can be swapped out for others like Fastify.
- Handlebars (hbs): A simple templating engine that allows you to dynamically render HTML pages.
This course assumes you already have a basic understanding of TypeScript and some familiarity with HTTP concepts. If you're comfortable with these technologies, you'll be well-prepared to follow along with the lessons.
To start working with NestJS, you need to install the Nest CLI globally on your machine. The CLI helps scaffold a new project and provides several useful commands for development:
Bash1$ npm i -g @nestjs/cli 2$ nest new project-name
These commands will install the Nest CLI and create a new project with the name you provide. However, in the practice section, you’ll find that we’ve already set up a NestJS environment for you, so you can focus on understanding the project structure and writing some code.
When you create a new NestJS project, several files and directories are generated. Let’s take a look at the five key files created and their roles:
- src/
- app.controller.spec.ts: A test file for the
AppController
. It uses Jest for testing the controller’s functionality. - app.controller.ts: This file defines the
AppController
, responsible for handling incoming requests and returning responses. - app.module.ts: The root module of your application, which organizes controllers, providers (like services), and other modules.
- app.service.ts: This file defines the
AppService
, which contains the business logic used by theAppController
. - main.ts: The entry point of the application where the NestJS app is bootstrapped and configured.
- app.controller.spec.ts: A test file for the
Throughout the upcoming units, we’ll dive deeper into what each of these files does, but for now, let's focus on the main.ts
file where the program bootstraps (or starts).
The main.ts
file serves as the entry point of your NestJS application. Let’s break down its contents:
TypeScript1import { NestFactory } from '@nestjs/core'; 2import { AppModule } from './app.module'; 3import { NestExpressApplication } from '@nestjs/platform-express'; 4import { join } from 'path'; 5import * as hbs from 'hbs'; 6 7async function bootstrap() { 8 const app = await NestFactory.create<NestExpressApplication>(AppModule); 9 app.setBaseViewsDir(join(__dirname, '..', 'views')); 10 app.setViewEngine('hbs'); 11 hbs.registerPartials(join(__dirname, '..', 'views/partials')); 12 await app.listen(3000); 13} 14bootstrap();
-
Import Statements: Here, we import essential modules like
NestFactory
for creating the app instance,AppModule
as the root module, and other libraries for setting up the view engine. -
Creating the Application: We use
NestFactory
to create an instance ofNestExpressApplication
withAppModule
as the starting point. -
Setting Up the View Engine: The
setBaseViewsDir
andsetViewEngine
methods configure the app to use Handlebars (hbs
) for rendering HTML views. -
Running the Application: To run the application, use the following command in your terminal:
Bash1$ npm run start
-
Listening on Port 3000: The
listen(3000)
function makes the application listen onhttp://localhost:3000
, which is where you can access the running app in your browser.
Now that we’ve covered the basics, let's move to the practice section and see a basic NestJS application in action.