Getting Started with NestJS: Building Your First Application

·

2 min read

NestJS brings a blend of familiar concepts from Angular, TypeScript's static typing, and dependency injection to the world of server-side development. Its robust architecture promotes code organization and reusability, making it an excellent choice for both small projects and large-scale applications.

Prerequisites

Before we begin, ensure you have Node.js and npm (Node Package Manager) installed on your system. You can download them from the official Node.js website

Installation

To create a new NestJS application, we'll use the Nest CLI, which simplifies project setup. Open your terminal and run the following command to install the Nest CLI globally:

npm install -g @nestjs/cli

Creating a New Application

Once the CLI is installed, you can create a new NestJS application with a single command:

nest new my-nest-app

Replace "my-nest-app" with your desired project name. This command will generate a new project structure with all the necessary files and configurations.

Exploring the Project Structure

Navigate to your newly created project:

cd my-nest-app

You'll find a well-organized project structure with folders such as src, and node_modules. The src folder houses your application's source code.

Hello, NestJS!

Let's create a simple "Hello, NestJS!" endpoint. In the src folder, find the app.controller.ts file. Replace its contents with the following code:

import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  getHello(): string {
    return 'Hello, NestJS!';
  }
}

Starting the Application

Now, run the application using the following command:

npm run start

Visit http://localhost:3000 in your browser, and you'll see the message "Hello, NestJS!" displayed.

Conclusion

Congratulations! You've successfully created and run your first NestJS application. This is just the beginning of your NestJS journey. As you continue exploring, you'll discover the framework's powerful features for building robust and scalable applications.
Follow me on Twitter @DesmondNyamador