DEV Community

Cover image for Getting Started with NestJS: A Beginner's Tutorial
Smartniggs
Smartniggs

Posted on • Updated on

Getting Started with NestJS: A Beginner's Tutorial

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:
    $ npm i -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode
  1. Run the following command to create a new NestJS project:
   $ nest new my-nest-app
Enter fullscreen mode Exit fullscreen mode
  1. Open newly created project folder

    $ cd my-nest-app
    
  2. Install dependencies:

    $ 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
Enter fullscreen mode Exit fullscreen mode
  • 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:
   import { Controller, Get } from '@nestjs/common';

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

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

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:
   $ npm run start
Enter fullscreen mode Exit fullscreen mode

This command will compile and run the application in development mode.

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

Top comments (0)