Chapter 2. Fundamentals

In Chapter 1, we created Sinatra applications that had a single HTTP endpoint exposed as a route. In this chapter, we’ll explore the common HTTP verbs and their usage, how to construct more complex routes around those verbs, how to include static resources (such as raw HTML pages, stylesheets, images, and JavaScripts), and how to create dynamic HTML views. We’ll also cover working with HTTP headers, configuration blocks, and filters.

Note

This chapter will be the most reference-y of the book, with each topic covered using the briefest examples possible to avoid cluttering the discussion with unrelated facets. The next few chapters will delve into the more theoretical and architectural aspects of Sinatra, then we’ll turn our attention to practical application and create several projects that are more involved and cover a wider breadth of knowledge.

Routing

The core of any Sinatra application is the ability to respond to one or more routes. Routes are the primary way that users interact with your application. To understand how Sinatra handles routing, we must first examine HTTP, the Hypertext Transfer Protocol.

Note

The discussion takes place from the perspective of the server; a request is created by a client (which may be a browser, another web application, etc.), and a response is created by the server and sent back.

This is also true with regard to operating on request and response objects in Sinatra, which we will discuss throughout this chapter; the former ...

Get Sinatra: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.