DEV Community

Alex Spinov
Alex Spinov

Posted on

NestJS Has a Free API You Should Know About

NestJS gives you enterprise-grade architecture with a powerful module system that goes far beyond simple REST endpoints.

Controllers & Services

@Controller('users')
export class UsersController {
  constructor(private usersService: UsersService) {}

  @Get()
  findAll(@Query('limit') limit = 10) {
    return this.usersService.findAll(limit)
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.usersService.findOne(id)
  }

  @Post()
  @UsePipes(new ValidationPipe())
  create(@Body() dto: CreateUserDto) {
    return this.usersService.create(dto)
  }
}
Enter fullscreen mode Exit fullscreen mode

Guards — Declarative Auth

@Injectable()
export class JwtGuard implements CanActivate {
  constructor(private jwtService: JwtService) {}

  async canActivate(context: ExecutionContext) {
    const request = context.switchToHttp().getRequest()
    const token = request.headers.authorization?.split(' ')[1]
    if (!token) throw new UnauthorizedException()
    request.user = await this.jwtService.verify(token)
    return true
  }
}

// Apply to controller
@UseGuards(JwtGuard)
@Controller('admin')
export class AdminController { /* ... */ }
Enter fullscreen mode Exit fullscreen mode

Interceptors — Transform Everything

@Injectable()
export class CacheInterceptor implements NestInterceptor {
  constructor(private cache: CacheService) {}

  async intercept(context: ExecutionContext, next: CallHandler) {
    const key = context.switchToHttp().getRequest().url
    const cached = await this.cache.get(key)
    if (cached) return of(cached)

    return next.handle().pipe(
      tap(data => this.cache.set(key, data, 300))
    )
  }
}
Enter fullscreen mode Exit fullscreen mode

WebSockets + GraphQL + Microservices

// WebSocket gateway
@WebSocketGateway()
export class EventsGateway {
  @SubscribeMessage('message')
  handleMessage(@MessageBody() data: string): string {
    return `Echo: ${data}`
  }
}

// GraphQL resolver
@Resolver('User')
export class UsersResolver {
  @Query(() => [User])
  users() { return this.usersService.findAll() }

  @Mutation(() => User)
  createUser(@Args('input') input: CreateUserInput) {
    return this.usersService.create(input)
  }
}
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case

A startup's Express API grew to 200 endpoints with spaghetti middleware. They refactored to NestJS modules: auth module, users module, billing module — each with its own guards, interceptors, and tests. Onboarding new developers went from 2 weeks to 2 days because the structure was self-documenting.

NestJS is Angular's architecture applied to backend — and it works brilliantly.


Build Smarter Data Pipelines

Need to scrape websites, extract APIs, or automate data collection? Check out my ready-to-use scrapers on Apify — no coding required.

Custom scraping solution? Email me at spinov001@gmail.com — fast turnaround, fair prices.

Top comments (0)