My name is Mason Ellwood, and I’m currently working on Flatiron School’s Online Full Stack Web Development Program. Each week, I’ll be writing about my experience, what I’m learning, and tips on learning to code.
If you have any Ruby experience you may be wondering why we have not gotten into Ruby on Rails yet…. Which is a valid question because of all the tools that come preset with it. But try to think of is this way. In the school, they cover setting up an ORM from scratch and building out your database. This takes a lot of time and energy and tracks through an extensive learning curve to complete. Setting up Sinatra automates this process. So why didn’t we just start with Sinatra?
Without knowing what an ORM does precisely, then we may not quite understand why Sinatra is breaking. Which it will do, one of the main jobs of a developer is to break and fix what is broken. Without first knowing what an ORM is and how it interacts with Ruby and your server, you may find it a lot harder to understand how to fix it. Spend time to dive into your code and understand the core concepts before jumping the gun, which could bite you in the end.
The same goes with The Flatiron School’s extensive dive into Rack. Why don’t they just jump to Rails which automates what Rack does for us? Well because like we said above you have to look at your learning curve in steps. Without knowing what is going on under the hood, you may have problems fixing your program when it breaks.
With Rack you will be able to better understand how it interacts with HTTP and once you step into the Rails world knowing how Rack is translating that information will greatly benefit you. So why use Rack? When looking at something like Facebook and Youtube it is hard to understand all the moving parts. This is where Rack comes in. Rack at its simplest level sets up a connection with the server and is what Rails is built on top of. Before we get to Rails lets better understand why Rack does and how it interacts with HTTP.
To interact with Rack we need to create a class that responds to a single method. This method will return a status code, any headers, and the body. All this can be done with the Rack::Response object. When setting this this response always end in a #finish. By default Rack set up your status code for you.
The code above will run whenever there is a request received. With the code above we need to set up one more system that connects you to that HTTP request, which sends it through to your #call method, which then serves that responds to the browser. We do this with the use of a config.ru file and the rack command.
Running this code we then run rack up config.ru
The port that is returned to you will set up a local environment (http://localhost:9292/). Using this url will display your data in the browser. The localhost is the server name on your computer.
And just like that, you have made a connection with the server! I know this was a weird article and it may not make sense. We will get into the file structure later on, but I hope this clears up some of what Rack is and what its purpose is. Thank you for reading, I will see you next week to dive a little deeper.
Leave a Reply