Getting Started with NestJS: A Beginner's Tutorial
Tags: tutorial, typescript, nestjs, node
Published: 2022-12-23 19:25:49

NestJS is a powerful and popular Node.js framework that is built with a focus on developer productivity and scalability. It provides a robust set of tools and architectural patterns to help you build efficient and maintainable server-side applications. 
In this tutorial, we will walk you through the process of getting started with NestJS and building your first application.

#### Prerequisites:
Before we dive into NestJS, make sure you have the following prerequisites:

1. Node.js installed on your machine (version 12 or higher).
2. A basic understanding of JavaScript or TypeScript.
3. Familiarity with Node.js and npm (Node Package Manager).

Let's get started!

#### Setting up a new NestJS project
1. Open your terminal and install the Nest CLI(Command line interface) globally:
  ```bash
	$ npm i -g @nestjs/cli
   ```
2. Run the following command to create a new NestJS project:
   ```bash
   $ nest new my-nest-app
   ```
3. Open newly created project folder
	```bash
   $ cd my-nest-app
   ```
4. Install dependencies:
	```bash
   $ npm install
   ```

#### Exploring the project structure
1. Open the project in your preferred code editor. You will see a folder structure similar to the following:
   ```
   my-nest-app
   ├── src
   │   ├── app.controller.ts
   │   ├── app.module.ts
   │   └── main.ts
   ├── test
   ├── .eslintrc.js
   ├── nest-cli.json
   ├── package.json
   ├── README.md
   ├── tsconfig.build.json
   └── tsconfig.json
   ```
   - `src/` folder contains the application's source code.
   - `app.controller.ts` is a sample controller file.
   - `app.module.ts` is the root module of the application.
   - `main.ts` is the entry point of the application.

#### Creating a basic controller and module
1. Open `src/app.controller.ts` in your code editor and replace the existing code with the following:
   ```typescript
   import { Controller, Get } from '@nestjs/common';

   @Controller()
   export class AppController {
     @Get()
     getHello(): string {
       return 'Hello NestJS!';
     }
   }
   ```
2. Open `src/app.module.ts` and replace the existing code with the following:
   ```typescript
   import { Module } from '@nestjs/common';
   import { AppController } from './app.controller';

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

#### Running the NestJS application
1. Open your terminal and navigate to the root directory of your project.
2. Run the following command to start the NestJS application:
   ```bash
   $ npm run start
   ```
   This command will compile and run the application in development mode.
3. Open your browser and navigate to `http://localhost:3000`. You should see the message "Hello NestJS!" displayed on the page.

Congratulations!!! You have successfully created and run your first NestJS application.

#### Conclusion:
In this tutorial, we covered the basics of getting started with NestJS. We set up a new project, explored the project structure, and created a basic controller and module. We also ran the application and saw the result in the browser.

NestJS provides a solid foundation for building scalable and maintainable server-side applications. As you dive deeper into NestJS, 

Thank you for reading.
 Feel free to reach out to me at smartniggs[a]gmail[.]com