Getting Started with NestJS - Installing NestCLI and Understanding the Basic Structure of a NestJS Application
- Published on
NestJS is a powerful, progressive Node.js framework for building efficient, scalable, and maintainable server-side applications. It leverages TypeScript and incorporates concepts from object-oriented programming, functional programming, and functional reactive programming.
We’ll go through the installation of the NestCLI and give a brief overview of a basic NestJS application structure.
Install NestCLI
Before you start building with NestJS, you need to install the NestCLI, which makes it easy to create and manage NestJS projects.
You can install the NestCLI globally using npm:
npm install -g @nestjs/cli
To create a new NestJS project, use the nest command followed by the new command:
nest new app_name
You’ll be prompted to choose a package manager (npm, yarn, or pnpm). Select the one you prefer, and the CLI will scaffold a new NestJS project for you.
Understanding the Basic Structure of a NestJS Application
After creating your project, navigate into your project directory:
cd app_name
Here’s a quick overview of the main files and directories:
- src/ Directory
The src/ directory contains all your application code.
app.controller.ts: This file defines a basic controller, responsible for handling incoming requests and returning responses.
app.service.ts: A service provider that handles business logic and is injected into the controller.
app.module.ts: The root module of the application. It organizes the application by grouping related controllers and services.
main.ts: The entry point of the application. This file bootstraps the Nest application.
- test/ Directory
This directory contains the unit and end-to-end tests for your application.
- node_modules/ Directory
As in any Node.js project, this directory contains all the dependencies for your project.
- package.json
The package.json file manages dependencies and scripts for your project. You’ll find the start scripts and any installed packages listed here.
- nest-cli.json
This configuration file is specific to NestJS projects. It defines the structure and build options for your Nest application.
Running NestJS Application
To run your application, simply use the following command:
npm run start
By default, the application will run on http://localhost:3000. You can visit this URL in your browser to see your running NestJS app.