Lesson 1
Very First App and Project Structure
First Steps

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.

Technologies Used

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.

Setup and Installation

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:

Bash
1$ 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.

Project Files and Directory Structure

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 the AppController.
    • main.ts: The entry point of the application where the NestJS app is bootstrapped and configured.

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).

Understanding the Entry Point (main.ts)

The main.ts file serves as the entry point of your NestJS application. Let’s break down its contents:

TypeScript
1import { 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 of NestExpressApplication with AppModule as the starting point.

  • Setting Up the View Engine: The setBaseViewsDir and setViewEngine 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:

    Bash
    1$ npm run start
  • Listening on Port 3000: The listen(3000) function makes the application listen on http://localhost:3000, which is where you can access the running app in your browser.

Excited? Let's Move to Practice!

Now that we’ve covered the basics, let's move to the practice section and see a basic NestJS application in action.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.