Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
A custom exception filter catching HTTP errors.
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { Controller, Get } from '@nestjs/common';
@Catch(HttpException)
class HttpErrorFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
response.status(exception.getStatus()).json({ message: exception.message });
}
}
@Controller()
class AppController {
@Get('error') throwError() { throw new HttpException('Custom error', 400); }
}
@Module({ controllers: [AppController] })
class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new HttpErrorFilter());
await app.listen(3000);
}
bootstrap();NestJS is a progressive Node.js framework for building efficient, scalable, and maintainable server-side applications using TypeScript and modern JavaScript.
Origin & Creator
NestJS was created by Kamil Myśliwiec in 2017 to provide a structured framework for building maintainable Node.js server-side applications with TypeScript.
Industrial Note
NestJS is popular in enterprise backend ecosystems and microservice architectures, providing scalable, modular, and maintainable server-side solutions.