1. Creating a basic Sinatra Project
Building your first basic Sinatra project
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
When creating routes within our Sinatra application, we’re mapping requests. Until now we’ve looked at ‘GET’ Requests. These are the most common way that information is requested from a webserver and the type of request you’ll most often used. But, depending on what we’re doing we may use other kinds of requests.
Sinatra intercepts the HTTP requests coming to it and it allows you to map them to custom actions in Ruby code that do things and send a response. This is illustrated in the image below.
The way we map onto them is by defining an action in our Ruby code file like so:
To allow this to work, we need to include four bits of information about how the HTTP request is mapped onto an action:
GET
./
. This is handled in the case where we just type the server URL into the browser and don’t add anything onto the path.do
and completes at the end
and is represented by the code we write; andThere are technically 8 different HTTP methods:
For routing, you’ll only ever really need to deal with the first four.
get '/something' do
.. show something ..
end
post '/something' do
.. create something ..
end
put '/something' do
.. replace something ..
end
delete '/something' do
.. annihilate something ..
end
Notice that all four have the same ‘endpoint’ or name for the route, but they can each do different things. This gets really useful later. More on that when we get to databases and interacting with webhooks from other services!
This series guides you through creating your first Sinatra project and some of the features it offers for creating web-delivered apps
Building your first basic Sinatra project
Building your first basic Sinatra project
Routing with HTTP Methods explained
How to include named parameters in defined routes
Storing User Information in Sessions
How to return a HTTP error response
Using configuration blocks to store application wide variables