NestJS is an increasingly popular framework for building efficient, scalable Node.js server-side applications. It leverages TypeScript, which offers a type-safe development experience, and adopts the modular architecture of Angular, making it easier to organize your application code. In this guide, we will walk through the process of setting up a NestJS project step-by-step and create a simple REST API.
Before we dive into the setup, make sure you have the following tools installed:
Node.js (version 14 or above): Ensure that Node.js is installed by running node -v
in your terminal. If it’s not installed, download it from nodejs.org.
npm (Node Package Manager): npm is included with Node.js installation. You can verify its installation by running npm -v
.
Nest CLI: We’ll use the NestJS command-line interface (CLI) to streamline our project setup. Install it globally using the following command:
npm install -g @nestjs/cli
Once you have these prerequisites in place, you’re ready to start your NestJS project!
Let’s create a new NestJS project using the Nest CLI. Open your terminal and type:
nest new my-nest-project
Replace my-nest-project
with your desired project name. The CLI will prompt you to select a package manager; you can choose either npm or Yarn. After a few moments, you’ll see a new folder with your project structure set up!
Navigate into your project directory:
cd my-nest-project
Let’s briefly discuss the essential folders and files created by the CLI:
Now that you have your NestJS application set up, let's run it:
npm run start
Your server will start running on http://localhost:3000
. You can verify it's working by navigating to that URL in your web browser. By default, you should see a message like “Hello World!”
To get a better understanding of NestJS, let’s create a new controller that returns a simple greeting. You can generate a new controller using the Nest CLI:
nest generate controller greeting
This command creates a greeting
folder inside src/
with a default controller file named greeting.controller.ts
.
Open the newly created controller file src/greeting/greeting.controller.ts
. It should contain a basic structure like this:
import { Controller, Get } from '@nestjs/common'; @Controller('greeting') export class GreetingController { @Get() getGreeting(): string { return 'Hello, welcome to NestJS!'; } }
greeting
.getGreeting
method, which returns a simple greeting message.In order to use this controller, you need to register it in the application module located in src/app.module.ts
. Update the AppModule
like so:
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { GreetingController } from './greeting/greeting.controller'; @Module({ imports: [], controllers: [AppController, GreetingController], providers: [AppService], }) export class AppModule {}
Now that your greeting controller is set up and registered, you can test it. Make sure your NestJS application is running, and navigate to http://localhost:3000/greeting
in your web browser or a tool like Postman.
You should see the message:
Hello, welcome to NestJS!
This confirms that your controller is functioning correctly!
Setting up a simple NestJS project is a straightforward process that sets the groundwork for building sophisticated backend applications. We've walked through creating a new project, running the server, and implementing your first controller, giving you a running start in backend development with NestJS. From here, you can explore more advanced features like services, middleware, and database integration to further elevate your backend skills.
10/12/2024 | NestJS
10/12/2024 | NestJS
10/12/2024 | NestJS
10/12/2024 | NestJS
10/12/2024 | NestJS
10/12/2024 | NestJS
10/12/2024 | NestJS
10/12/2024 | NestJS