The internet is just "client sends an HTTP request to the server, which sends back an HTTP response" where client is usually web browser. Sending an HTTP request is like writing to a file that goes across the internet, and reading from a file that came from the internet. So to understand the web, the most important thing is to understand HTTP requests. Then, you can go to Sinatra, and it will be "this is how Sinatra does that underlying thing you already know".
To see the request, go to the terminal and type nc -l 3000
(the number is called a port, it's like an index in an array, you put a program there, then when a web request comes in, it checks the port, finds the program, and hands it the incoming request). The specific number isn't important, as long as its big enough to get past the claimed ones. Now that you have that running, set it on one side of your screen, and set your browser on the other side. Then type http://localhost:3000/whatever?abc=123
into the browser's URL bar.
So back in the first window, we can type the response in:
HTTP/1.1 303 i like bananas
Location: http://today.turing.io
You need the blank line or it will keep waiting for headers.
Play with this a bit, see what happens when you set Content-Type
to text/html
, and to text/plain
, how does the browser display the body (same body in both cases), try setting a cookie and seeing it come back in. Cookies are important, some students are going to lose like a week or 2 b/c they don't get what a cookie is and so they don't get what a session is or the flash is.
After you play with that enough to feel comfortable with it (also note that you can pop open the network tab of your browser and see that it makes sense of all these things). If you feel comfortable enough, you might try to just write a miniature version of Sinatra. Here is mine in 92 lines of code Fake Sinatra Basically all Ruby web frameworks use something called Rack to connect the server to the app, and all you have to know about that is that you connect the app to the framework in config.ru (line 42 of that file) and when a request comes in, it parses the text into a hash, which it gives the app in the call
method. The app does whatever, then gives back an array with the status code, the headers, and the body. All the rest of Sinatra is about giving the user a nice way to decide what code to run for the given request, and what headers/code/body to set into the response.