DEV Community

Cover image for Understanding Default NestJS Project Structure
Shameel Uddin
Shameel Uddin

Posted on

Understanding Default NestJS Project Structure

In previous blog, we understood the use-cases for Node.js, Express.js and Nest.js and then learned how to install Nest.js CLI and initiated our project.

In this blog, we will understand default nestjs project structure.

We will not be diving deep into any of the configuration files; rather, we'd discuss core files which we will be using for our nest.js series.

Video Explanation

Please find video explanation below ↓

Starting the project

In package.json file, you can find these two:

"start": "nest start",
"start:dev": "nest start --watch",
Enter fullscreen mode Exit fullscreen mode

If you use npm run start then whenever you make changes, you will have to restart your server. So, during development mode, always run this: npm run start:dev so that as soon as you make any changes in the file, the server restarts automatically.

Nest.js Default Core Files

When diving into a Nest project, the src/ directory plays host to several essential files. Let's take a closer look at each one:

main.ts:

As the gateway to your application, main.ts is where the Nest application instance comes to life and is registered to a specific port (i.e., 3000 by default), enabling access from your client apps:

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

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

app.module.ts:

This file takes center stage as the root module for your application. Every other module you create will find its registration within this file:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

app.controller.ts:

This file serves as the foundation for your controllers, featuring a basic controller class with a single route definition.

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

Enter fullscreen mode Exit fullscreen mode

app.controller.spec.ts:

This is the unit test file designed for the app.controller.ts class. It ensures the reliability of your controller through testing. Here's a snippet illustrating a basic test:

import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
  let appController: AppController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [AppService],
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should return "Hello World!"', () => {
      expect(appController.getHello()).toBe('Hello World!');
    });
  });
});
Enter fullscreen mode Exit fullscreen mode

app.service.ts:

Here, you'll encounter a fundamental service responsible for executing logic utilized by your controller:

import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello Shameel!';
  }
}
Enter fullscreen mode Exit fullscreen mode

Understanding the purpose of each core file empowers you to navigate and structure your Nest project effectively.

Follow me for more such content:
YouTube: https://www.youtube.com/@ShameelUddin123
LinkedIn: https://www.linkedin.com/in/shameeluddin/
Github: https://github.com/Shameel123

Top comments (0)