Building Scalable Server-Side Applications with NestJS
Understanding HTTP Requests
An HTTP request is a user’s request to perform an action related to a resource. These requests are made through a client, such as a browser or application, and are classified by verbs. The most commonly used verbs are:
- GET: Retrieve a resource or a list of resources
- POST: Create a new resource entry
- PUT: Update an existing resource
- DELETE: Delete an existing resource
Defining NestJS Routes and Controllers
In NestJS, a route is a combination of an HTTP method, a path, and the function to handle it defined in an application. A controller is a class defined with methods for handling one or more requests. The controller provides the handler function for a route. In NestJS, routes are defined in the controller alongside the handler method using decorators.
Creating a NestJS Project
To get started with NestJS, we need to create a new project. We can do this using the NestJS CLI. Once installed, we can run the Nest command to create a new project:
npx @nestjs/cli new nest-project
Setting up the Database and ORM
For our example application, we will use a MySQL database and TypeORM to handle operations to our DB. We will install the required packages and configure our database connection:
npm install --save @nestjs/typeorm typeorm mysql2
Creating Entities and Services
Next, we will create a Tree entity and a TreeService to handle operations on our Tree entity. We will use the repository design pattern to abstract communication with the data layer.
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Tree {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
Defining Routes and Controllers
Now that we have set up our database and entities, we can define our routes and controllers. We will create a TreeController to handle requests related to our Tree entity:
import { Controller, Get } from '@nestjs/common';
@Controller('trees')
export class TreeController {
@Get()
async getAllTrees() {
// Return all trees
}
}
Passing Parameters in Routes
We can pass parameters in routes using the @Param()
decorator. This allows us to access the parameter value in our handler function:
import { Controller, Get, Param } from '@nestjs/common';
@Controller('trees')
export class TreeController {
@Get(':id')
async getTree(@Param('id') id: string) {
// Return tree by id
}
}
Passing a Request with a Payload Body
We can pass a payload body in requests using the @Body()
decorator. This allows us to access the payload body in our handler function:
import { Controller, Post, Body } from '@nestjs/common';
@Controller('trees')
export class TreeController {
@Post()
async createTree(@Body() tree: Tree) {
// Create a new tree
}
}
Passing Query Parameters in Routes
We can pass query parameters in routes using the @Query()
decorator. This allows us to access the query parameter value in our handler function:
import { Controller, Get, Query } from '@nestjs/common';
@Controller('trees')
export class TreeController {
@Get()
async getTrees(@Query('name') name: string) {
// Return trees by name
}
}
Validating Route Parameters
We can validate route parameters using pipes. This ensures that the data sent via parameters matches what our application expects:
import { Controller, Get, Param, Pipe } from '@nestjs/common';
import { ValidationPipe } from './validation.pipe';
@Controller('trees')
export class TreeController {
@Get(':id')
async getTree(@Param('id') id: string, @Pipe(ValidationPipe) tree: Tree) {
// Return tree by id
}
}