,

أفضل إطار عمل Back-End في الجافا سكريبت Nest.js 🚀🔴

Posted by


Nest.js is a powerful and flexible back-end framework for building server-side applications with JavaScript or TypeScript. It is built on top of Node.js and offers a modern and modular approach to building applications.

In this tutorial, we will discuss how to set up and use Nest.js as your preferred backend framework in JavaScript.

Step 1: Install Nest.js

To get started with Nest.js, you’ll need to have Node.js installed on your machine. You can download and install Node.js from the official website (https://nodejs.org/).

Once you have Node.js installed, you can install Nest.js using npm (Node Package Manager) by running the following command:

npm install -g @nestjs/cli

This command will install the Nest CLI globally on your machine, allowing you to create new Nest projects and manage existing ones.

Step 2: Create a new Nest.js project

To create a new Nest.js project, run the following command in your command line interface:

nest new my-nest-project

Replace my-nest-project with the name of your project. This command will generate a new Nest.js project with all the necessary files and folders to get you started.

Step 3: Run the Nest.js project

Once your project is created, navigate to the project folder and run the following command to start the development server:

cd my-nest-project
npm run start:dev

This command will start the Nest.js development server and you can access your application by navigating to http://localhost:3000 in your web browser.

Step 4: Create a controller

In Nest.js, controllers are responsible for handling incoming requests and returning responses. To create a new controller, run the following command:

nest generate controller cats

This command will generate a new controller file in the src/cats folder with some sample code to get you started.

Step 5: Create a service

Services in Nest.js are used to handle business logic and data manipulation. To create a new service, run the following command:

nest generate service cats

This command will generate a new service file in the src/cats folder with some sample code.

Step 6: Create a module

Modules in Nest.js are used to organize and encapsulate related controllers, services, and providers. To create a new module, run the following command:

nest generate module cats

This command will generate a new module file in the src/cats folder with some sample code.

Step 7: Connect the controller, service, and module

To connect the controller, service, and module, import the controller and service into the module file and add them to the providers and controllers arrays:

import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';

@Module({
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}

Step 8: Dependency injection

In Nest.js, services and providers are injected into controllers using dependency injection. To inject a service into a controller, import the service class into the controller file and add it to the constructor:

import { Controller } from '@nestjs/common';
import { CatsService } from './cats.service';

@Controller('cats')
export class CatsController {
  constructor(private readonly catsService: CatsService) {}
}

Step 9: Define routes

Routes in Nest.js are defined using decorators on controller methods. To define a route, add a method to the controller class with the @Get, @Post, @Put, or @Delete decorator and specify the route path:

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

@Get(':id')
async findOne(@Param('id') id: string) {
  return this.catsService.findOne(id);
}

Step 10: Test the application

To test your Nest.js application, navigate to http://localhost:3000/cats in your web browser to access the default route defined in the CatsController.

Congratulations! You have successfully set up and created a basic Nest.js application with a controller, service, and module. You can now expand upon this foundation to build more complex and feature-rich applications using this powerful back-end framework. Happy coding! 😼🔴

0 0 votes
Article Rating

Leave a Reply

16 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@iM2030_
3 hours ago

قسم بالله تعبنا من كثر المكتبات والملحقات والفريم وورك.. أروح افتح لي بسطة خضار أرحم من السهر والتدريب وكثرة الأكواد

@AhmadKoudier
3 hours ago

اتفق وبقوة, قريب كتير من اللارفيل

@abdallahhasan9371
3 hours ago

nestjs has alot of issues and community still not that good, stick with nodejs guys

@pactube8833
3 hours ago

يحيى شو رأيك ب Deno 2؟ الإعلان كان رهيب

@SUPERXNERO
3 hours ago

nest.js او Next.js
ما الافضل يا استاذ؟

@mohammedwafy2411
3 hours ago

شوية كمان هيطلع حاجة تبني على ال nestjs وهكذا
وفي الاخر كله بيرسم شوية html ..!!!!

@mitanomar
3 hours ago

جماعة ال nodejs في اطار عمل اسمه adonisjs.
بعد ما تستعمله، ما تحتاج شي تاني
كود منظم مثل هيكلية لارافل
مستخدم عندي في اكثر من مشروع

@Anna-ku9wc
3 hours ago

اذا بتعلم node.js او laravel هل يحتاج اكون متعلم قبلها الجافا سكريب و php ومتعمق فيها ولا ينفع اساسيات واقترحو عليه كورسات لهذا الموضوع؟

@brahimaitomghar7880
3 hours ago

Hono المستقبل >>>>>>

@NaifIT11
3 hours ago

NestJS
زي Angular
اصلا هم اخذوا افكار angular😂بس للbackend

@kosaykm9087
3 hours ago

Node js ولا laravel

@SalaheddinKatibi
3 hours ago

يا اخي انت تقرى افكاري ولا ايه 😂 انا كل مرى اطرح سؤال على بالي الاقيك مزل حل للمشكلة

@KhaledGhallabPro
3 hours ago

جرب Hono يا يحي وهتنسي NestJs بالتوفيق

@Billion_Millionaire
3 hours ago

على فكرة انت حيرتني انا مش لاقي كورسات اتعلم زي node.js فحاول تعطينا الصورة كاملة عشان أملنا يعتمد على وجود الهدف في علبة واحدة وليست البحث عنه لأن المشتتات كتيرة

@ammarkassem9976
3 hours ago

اذا سمحت يا استاذ ممكن تتكلم عن لارافيل او تنزل كورس
انا عمري ١٥ سنة و بشتغل ويب و رح اتعلم لارافيل اذا نزلت كورس حضرت

@AREOPAGOO
3 hours ago

👍👍👍👍👍

16
0
Would love your thoughts, please comment.x
()
x