← Back to list

NestJS — Inspired by Angular Framework(part 2)

In the previous post, we explored what NestJS is, how to set it up, and its folder structure. In this post, we will dive into creating…

Aaranya · 2026-04-15 17:01 · 0 claps · 2.8 min read
#nestjs #nestjs-tutorial #nestjs-module #nestjs-controller #backend-development
Open on Medium ↗
Wiki topics: 🌐 · Web Development

NestJS — Inspired by Angular Framework(part 2)

In the previous post, we explored what NestJS is, how to set it up, and its folder structure. In this post, we will dive into creating modules, controllers through practical, basic examples.

Modules:

Modules are the fundamental building blocks of a NestJS application. They encapsulate related controllers, services, providers, and other components to ensure code organization, reusability, and testability.

Modularity: Modules break the application down into smaller, self-contained units, making the codebase easier to manage and understand.

Dependency Injection (DI): NestJS uses DI to provide modules with their necessary dependencies, promoting loose coupling and easier testing.

Providers: Providers are the “workers” of your application that handle all the heavy lifting and business logic. They are designed to be injected into controllers or other services, so you don’t have to manually create them every time.

every modules consist

— controller

— service

— test file(spec)

— Module

Create Module:

To create a new module via the NestJS CLI, use the following command:

nest g module modulename or nest g mo modulename

the NestJS CLI doesn’t just create the files — it also automatically registers the new module. It locates the app.module.ts file and adds your new module to the @Module() decorator’s imports array.

Create Controllers:

Controllers are used to handling incoming Http Request and outgoing Http Responses.

Routing: They define specific URL paths (e.g., /users or /products) that the application should listen to.

Request Handling: They extract data from the request, such as headers, query parameters, or the body.

Response Formatting: They ensure the client receives the correct data and HTTP status codes (like 200 OK or 404 Not Found).

@Controller(‘employee’) =>http://localhost:3000/employee

You can generate a controller quickly using the NestJS CLI with either of these commands:

nest g controller controllername or nest g co controllername

Routing decorators in NestJS are used to define the specific endpoints your application responds to. By placing these decorators above a function in a controller, you map a specific HTTP Method and URL Path to that function.

Common Methods:

@Get(): Used for retrieving data. (e.g., fetching a list of users).

@Post(): Used for creating new data. (e.g., registering a new user).

@Put(): Used for updating an entire resource.

@Patch(): Used for partial updates to a resource (e.g., changing only a particular value).

@Delete(): Used for removing data.

Example:

@Get()

getEmployeeDetails() {

return “can get employee details.”;

}

Route Parameter:

Route Parameters are placeholders within a URL that allow you to capture dynamic data. They are essential when you need to identify a specific resource, such as a specific user or a product ID.

Example Breakdown: http://www.domain.com/employedetails/1

  • http://www.domain.com: Domain
  • employedetails: The Resource/Route path
  • 1: The Route Parameter (the dynamic ID)

How to access Route Parameters

To capture a parameter, you use a colon (:) in the @Get decorator and the @Param() decorator in the function signature.

import { Param } from ‘@nestjs/common’;

Method 1: Capturing a specific value

If you specify the name inside @Param(‘id’), NestJS gives you the exact value as a string.

TypeScript

@Get(‘:id’) // The colon indicates a dynamic parameter

getEmployeeDetails(@Param(‘id’) id: string) {

console.log(id); // Output: 1

}

Method 2: Capturing the entire params object

If you don’t pass a name into @Param(), NestJS returns an object containing all the route parameters.

TypeScript

@Get(‘:id’)

getEmployeeDetails(@Param() params: any) {

console.log(params); // Output: { id: ‘1’ }

}

if we have multiple parameters also we can get as

{id:”1",name:”malar”}

QueryStrings:

Query Strings are optional key-value pairs added to the end of a URL after a question mark (?). They are typically used for filtering, sorting, or pagination — actions that don’t change the main resource but modify how the data is presented.

Example Breakdown: http://www.domain.com/employees?department=IT&sort=asc

  • ?: Indicates the start of the query string.
  • department=IT: A key-value pair where the key is “department” and the value is “IT”.
  • &: Used to separate multiple query parameters.

How to access Query Strings in NestJS

To capture these values, you use the @Query() decorator in your controller method.

import { Query } from ‘@nestjs/common’;

Method 1: Capturing a specific value

If you want to grab just one specific query parameter, pass the name of the key into the decorator.

@Get()

findAll(@Query(‘department’) dept: string) {

console.log(dept); // Output: IT

}

Method 2: Capturing the entire Query object

If you have multiple parameters and want to access them all at once, leave the decorator empty.

@Get()

findAll(@Query() query: any) {

console.log(query); // Output: { department: ‘IT’, sort: ‘asc’ }

}


메타데이터
post_id
a23502f845d5
slug
nestjs-inspired-by-angular-framework-part-2-a23502f845d5
url
https://medium.com/@malarcse2015/nestjs-inspired-by-angular-framework-part-2-a23502f845d5
canonical_url
https://medium.com/@malarcse2015/nestjs-inspired-by-angular-framework-part-2-a23502f845d5
author_url
https://medium.com/@malarcse2015
status
ok
fetched_at
2026-08-24 15:44:55