logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Setting Up a NestJS Project

author
Generated by
ProCodebase AI

10/12/2024

NestJS

Sign in to read full article

Introduction to NestJS

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.

Prerequisites

Before we dive into the setup, make sure you have the following tools installed:

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

  2. npm (Node Package Manager): npm is included with Node.js installation. You can verify its installation by running npm -v.

  3. 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!

Creating a New 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!

Project Structure Overview

Navigate into your project directory:

cd my-nest-project

Let’s briefly discuss the essential folders and files created by the CLI:

  • src/: Contains the main application code, including modules, controllers, and services.
  • main.ts: The entry point of the application.
  • app.module.ts: The root module that connects various parts of the application.
  • app.controller.ts: The default controller created with the project.
  • app.service.ts: The default service that provides data for the controller.

Running Your NestJS Application

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!”

Creating Your First Controller

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.

Setting Up the Greeting Controller

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!'; } }
  • @Controller('greeting'): This decorator defines a new controller with the route prefix greeting.
  • @Get(): This decorator maps HTTP GET requests to the getGreeting method, which returns a simple greeting message.

Registering the Greeting Controller in the Module

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 {}

Testing Your New Route

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!

Conclusion

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.

Popular Tags

NestJSBackend DevelopmentNode.js

Share now!

Like & Bookmark!

Related Collections

  • NestJS Mastery: Modern Backend Development

    10/12/2024 | NestJS

Related Articles

  • Microservices with NestJS

    10/12/2024 | NestJS

  • Understanding Interceptors in NestJS

    10/12/2024 | NestJS

  • Dependency Injection in NestJS

    10/12/2024 | NestJS

  • Working with Databases using TypeORM in NestJS

    10/12/2024 | NestJS

  • Testing in NestJS using Jest

    10/12/2024 | NestJS

  • Performance Optimization in NestJS Applications

    10/12/2024 | NestJS

  • Validation with Class Validator and Class Transformer in NestJS

    10/12/2024 | NestJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design