1. Home
  2. /
  3. Sinatra
  4. /
  5. Simple Counter App

Simple Counter App - Sinatra Typing CST Test

Loading…

Simple Counter App — Sinatra Code

Demonstrates a simple Sinatra app with a counter using in-memory state and route handlers.

require 'sinatra'

count = 0

get '/counter' do
	"<h2>Counter: #{count}</h2>
	<form method='post' action='/increment'>
		<button type='submit'>+</button>
	</form>
	<form method='post' action='/decrement'>
		<button type='submit'>-</button>
	</form>
	<form method='post' action='/reset'>
		<button type='submit'>Reset</button>
	</form>"
end

post '/increment' do
	count += 1
	redirect '/counter'
end

post '/decrement' do
	count -= 1
	redirect '/counter'
end

post '/reset' do
	count = 0
	redirect '/counter'
end

# Run with: ruby app.rb -p 4567

Sinatra Language Guide

Sinatra is a lightweight, domain-specific Ruby web framework for quickly creating web applications and APIs with minimal boilerplate, emphasizing simplicity and flexibility.

Primary Use Cases

  • ▸Building simple web applications quickly
  • ▸Creating RESTful APIs
  • ▸Prototyping services and applications
  • ▸Microservices for modular architectures
  • ▸Lightweight web apps that don’t need full Rails stack

Notable Features

  • ▸DSL for defining HTTP routes and handlers
  • ▸Rack-based middleware support
  • ▸Built-in request and response handling
  • ▸Template rendering with ERB, Haml, or Slim
  • ▸Lightweight, fast, and minimalistic

Origin & Creator

Created by Blake Mizerany in 2007 as a minimal Ruby web framework alternative to Rails.

Industrial Note

Widely used in lightweight web services, microservices, RESTful APIs, and quick prototypes, often in startups or for internal tooling.

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher