NestJS: How to start NestJS

NestJS: How to start NestJS

NestJS is a framework for building scalable and efficient server-side applications with Node.js. It follows the modular design pattern and comes with built-in support for TypeScript, making it a popular choice among developers.

In this article, we will walk you through the process of getting started with NestJS. We will cover the installation, project setup, and building a simple API using NestJS.

Prerequisites

Before we get started, make sure you have the following tools installed on your machine:

  • Node.js (version 12 or above)
  • npm (Node Package Manager)

You can install Node.js from the official website: https://nodejs.org

Installation

To install NestJS, open your terminal and run the following command:

npm install -g @nestjs/cli

The above command will install the NestJS CLI globally on your machine. This CLI tool helps you create and manage NestJS applications.

Creating a New NestJS Project

Now that NestJS is installed, let’s create a new project. Navigate to the desired directory where you want to create your project and run the following command:

nest new my-nest-app

Replace my-nest-app with the name of your project. This command will create a new NestJS project with the specified name.

Project Structure

After the project is created, you will see a directory structure similar to the following:

my-nest-app
  ├── src
  |   ├── main.ts
  ├── test
  ├── .gitignore
  ├── nest-cli.json
  ├── package.json
  ├── README.md
  ├── tsconfig.build.json
  └── tsconfig.json

Here’s a brief explanation of the important files and directories:

  • src: Contains the source code of your NestJS application.
  • main.ts: The entry point of your application.
  • test: Contains the test files.
  • nest-cli.json: Configuration file for NestJS CLI.
  • package.json: Contains the dependencies and scripts for your application.
  • README.md: Readme file for your project.

Building a Simple API

Now let’s create a simple API using NestJS. Open the src directory and create a new file called cats.controller.ts.

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

@Controller('cats')
export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }
}

In the above code, we have created a new controller called CatsController with a single GET endpoint. The findAll method returns a string indicating that it returns all cats.

Next, open the src/main.ts file and update it as follows:

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

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

This code sets up the NestJS application and listens for incoming requests on port 3000.

To start your NestJS application, run the following command:

npm run start

You should see the following output in your terminal:

[Nest] 63442   - [NestFactory] Starting Nest application...
[Nest] 63442   - [InstanceLoader] AppModule dependencies initialized
[Nest] 63442   - [RoutesResolver] AppController {/}: +3ms
[Nest] 63442   - [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 63442   - [NestApplication] Nest application successfully started +2ms

Congratulations! Your NestJS application is now running.

Open your browser and navigate to http://localhost:3000/cats. You should see the following message: “This action returns all cats”.

Conclusion

In this article, we have covered the basics of getting started with NestJS. We learned how to install NestJS, create a new project, and build a simple API. Now you are ready to explore the powerful features of NestJS and build scalable applications with ease.

Happy coding!