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

Simple Counter API - Fastify Typing CST Test

Loading…

Simple Counter API — Fastify Code

Demonstrates a simple Fastify REST API with a counter using routes and state management.

const fastify = require('fastify')({ logger: true })

let count = 0

fastify.get('/counter', async (request, reply) => {
	return { count }
})

fastify.post('/counter/increment', async (request, reply) => {
	count++
	return { count }
})

fastify.post('/counter/decrement', async (request, reply) => {
	count--
	return { count }
})

fastify.post('/counter/reset', async (request, reply) => {
	count = 0
	return { count }
})

const start = async () => {
	try {
		await fastify.listen({ port: 3000 })
		console.log('Fastify server running on http://localhost:3000')
	} catch (err) {
		fastify.log.error(err)
		process.exit(1)
	}
}
start()

Fastify Language Guide

Fastify is a high-performance, low-overhead web framework for Node.js. It emphasizes speed, schema-based validation, and a developer-friendly plugin system for building APIs and backend services.

Primary Use Cases

  • ▸Building RESTful and JSON APIs
  • ▸High-performance backend for web/mobile apps
  • ▸Microservices architecture
  • ▸Server-side processing for IoT or real-time systems
  • ▸Integrating with TypeScript for strongly-typed APIs

Notable Features

  • ▸Extremely fast HTTP handling
  • ▸JSON schema validation & serialization
  • ▸Built-in logging with Pino
  • ▸Plugin architecture for modular design
  • ▸Supports asynchronous hooks and lifecycle events

Origin & Creator

Fastify was created by Matteo Collina and Tomas Della Vedova in 2017 as a modern, high-performance alternative to Express.js.

Industrial Note

Fastify is preferred for high-performance APIs, microservices, and backend systems where speed, schema validation, and low overhead are critical.

More Fastify Typing Exercises

Fastify Query Params ExampleFastify URL Params ExampleFastify JSON Body ExampleFastify Middleware ExampleFastify Static Files ExampleFastify Router ExampleFastify Error Handling ExampleFastify CORS ExampleFastify Environment Variables Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher