NestJS is a framework for building Node.js server-side applications, fully supporting TypeScript and designed for creating scalable, maintainable applications. The document provides a detailed introduction to creating and managing a NestJS project, including examples of modules, controllers, services, middleware, and routing. It also highlights error handling by demonstrating a bad API call that triggers validation errors.
Introduction
NestJS
− is aframework for building Node.js server-side applications
− is built with and fully supports TypeScript
− allows developers to create testable, scalable, loosely coupled, and
easily maintainable applications
− the architecture is heavily inspired by Angular
/src/app.module.ts
import { Module} from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
9.
/src/main.ts
import { NestFactory} from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
10.
Nest CLI
# Compilesand runs an application in watch mode
$ nest start --watch
# Compiles an application or workspace into an output folder.
$ nest build
# Generates files based on a schematic
$ nest generate module cats
$ nest generate controller cats
$ nest generate service cats
...
A Bad APICall
GET /cats/abc
/* Nest will throw an exception */
{
"statusCode": 400,
"message": "Validation failed (numeric string is expected)",
"error": "Bad Request"
}