Back to Blog
Introduction to NestJS

4/1/20224 min read

Introduction to NestJS

NestJS is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications. Built with TypeScript and inspired by Angular, it provides a robust architecture out of the box.

💡 NestJS combines elements of OOP, FP, and FRP to give you a highly testable and maintainable application structure.

Why Use NestJS?

  • Built with TypeScript (full type safety)
  • Modular Architecture (dependency injection)
  • Express/Fastify under the hood
  • Built-in support for GraphQL, WebSockets, Microservices
  • Extensive CLI for code generation
  • Great for REST APIs and Microservices

Hello NestJS

Create a simple REST controller:

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

@Controller('hello')
export class HelloController {
  @Get()
  sayHello(): string {
    return 'Hello from NestJS!';
  }
}

Application Entry Point

typescript
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

Module Structure

typescript
import { Module } from '@nestjs/common';
import { HelloController } from './hello.controller';

@Module({
  controllers: [HelloController],
  providers: [],
})
export class AppModule {}

Dependency Management (npm)

json
{
  "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  }
}

Key Decorators

  • @Controller() - Defines a controller class
  • @Module() - Defines a module
  • @Get(), @Post(), @Put(), @Delete() - HTTP methods
  • @Injectable() - Marks a class as a provider
  • @Param(), @Body(), @Query() - Extract request data

Conclusion

With NestJS, you can build scalable, maintainable server-side applications with TypeScript. Its modular architecture and powerful CLI make it easy to structure complex applications.

🚀 Ready to take off? Try building your first CRUD API with NestJS!

Other posts that might interest you...