1. Home
  2. /
  3. Nestjs
  4. /
  5. Simple Counter API

Simple Counter API - Nestjs Typing CST Test

Loading…

Simple Counter API — Nestjs Code

Demonstrates a simple NestJS REST API with a counter using a service and controller.

import { Controller, Get, Post } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

class CounterService {
	private count = 0;
	getCount() { return this.count; }
	increment() { this.count++; return this.count; }
	decrement() { this.count--; return this.count; }
	reset() { this.count = 0; return this.count; }
}

@Controller('counter')
class CounterController {
	constructor(private readonly service: CounterService) {}
	@Get() getCount() { return { count: this.service.getCount() }; }
	@Post('increment') increment() { return { count: this.service.increment() }; }
	@Post('decrement') decrement() { return { count: this.service.decrement() }; }
	@Post('reset') reset() { return { count: this.service.reset() }; }
}

@Module({ controllers: [CounterController], providers: [CounterService] })
class AppModule {}

async function bootstrap() {
	const app = await NestFactory.create(AppModule);
	await app.listen(3000);
	console.log('NestJS server running on http://localhost:3000');
}
bootstrap();

Nestjs Language Guide

NestJS is a progressive Node.js framework for building efficient, scalable, and maintainable server-side applications using TypeScript and modern JavaScript.

Primary Use Cases

  • ▸Building RESTful APIs and GraphQL services
  • ▸Creating microservices with built-in support
  • ▸Developing server-side applications with TypeScript
  • ▸Implementing real-time WebSocket applications
  • ▸Building enterprise-grade backend systems

Notable Features

  • ▸TypeScript-first design
  • ▸Modular architecture
  • ▸Dependency Injection container
  • ▸Support for REST, GraphQL, WebSockets, and microservices
  • ▸Built-in testing and CLI tools

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.

More Nestjs Typing Exercises

NestJS Hello World APINestJS Simple Todo APINestJS Query Params ExampleNestJS POST JSON ExampleNestJS Middleware ExampleNestJS Exception Filter ExampleNestJS Param ExampleNestJS Service Injection ExampleNestJS Async Service Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher