Understanding Default NestJS Project Structure
Tags: nestjs, node, javascript, typescript
Published: 2023-12-27 12:08:50

In [previous blog](https://dev.to/shameel/getting-started-with-nestjs-cl1), 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 ↓

{% youtube 3q06vqkSI_A %}


## Starting the project

In `package.json` file, you can find these two:
```
"start": "nest start",
"start:dev": "nest start --watch",
```

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:

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

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

### 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:

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

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

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

```js
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();
  }
}

```
### 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:

```js
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!');
    });
  });
});
```


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

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

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello Shameel!';
  }
}
```


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