NestJS is a powerful and popular Node.js framework that is built with a focus on developer productivity and scalability. It provides a robust set of tools and architectural patterns to help you build efficient and maintainable server-side applications.
In this tutorial, we will walk you through the process of getting started with NestJS and building your first application.
Prerequisites:
Before we dive into NestJS, make sure you have the following prerequisites:
- Node.js installed on your machine (version 12 or higher).
- A basic understanding of JavaScript or TypeScript.
- Familiarity with Node.js and npm (Node Package Manager).
Let's get started!
Setting up a new NestJS project
- Open your terminal and install the Nest CLI(Command line interface) globally:
$ npm i -g @nestjs/cli
- Run the following command to create a new NestJS project:
$ nest new my-nest-app
-
Open newly created project folder
$ cd my-nest-app
-
Install dependencies:
$ npm install
Exploring the project structure
- Open the project in your preferred code editor. You will see a folder structure similar to the following:
my-nest-app
├── src
│ ├── app.controller.ts
│ ├── app.module.ts
│ └── main.ts
├── test
├── .eslintrc.js
├── nest-cli.json
├── package.json
├── README.md
├── tsconfig.build.json
└── tsconfig.json
-
src/
folder contains the application's source code. -
app.controller.ts
is a sample controller file. -
app.module.ts
is the root module of the application. -
main.ts
is the entry point of the application.
Creating a basic controller and module
- Open
src/app.controller.ts
in your code editor and replace the existing code with the following:
import { Controller, Get } from '@nestjs/common';
@Controller()
export class AppController {
@Get()
getHello(): string {
return 'Hello NestJS!';
}
}
- Open
src/app.module.ts
and replace the existing code with the following:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
@Module({
controllers: [AppController],
})
export class AppModule {}
Running the NestJS application
- Open your terminal and navigate to the root directory of your project.
- Run the following command to start the NestJS application:
$ npm run start
This command will compile and run the application in development mode.
- Open your browser and navigate to
http://localhost:3000
. You should see the message "Hello NestJS!" displayed on the page.
Congratulations!!! You have successfully created and run your first NestJS application.
Conclusion:
In this tutorial, we covered the basics of getting started with NestJS. We set up a new project, explored the project structure, and created a basic controller and module. We also ran the application and saw the result in the browser.
NestJS provides a solid foundation for building scalable and maintainable server-side applications. As you dive deeper into NestJS,
Thank you for reading.
Feel free to reach out to me at smartniggs[a]gmail[.]com
Top comments (0)