We all know that the internet sends and receives data. But how does it really work? Where is the information stored and how?
Sinatra is a web framework that helps build small web applications. with rack-based application, we can now handling HTTP request and response which is how the internet operates.
Within our code we will want to include
gem 'sinatra'. Then we will want to run "bundle install:" in our terminal to get the Sinatra gem from the Gemfile.
then you can create a file with any name and end it with a .rb ex: file_name.rb
within that file, include at the top
require 'sinatra' and in order to start a simple run we will include this code:
then in the terminal run ruby file_name.rb. Now in the output after running the .rb file we will see a line that is showing "listening on tcp::// XXX.X.X.X.XXXX. the last four digits will be the local host port.
putting locahost:XXXX into your browser will display the code we had inputted earlier. Now we have a simple Sinatra application running.
Sending HTML & JSON Responses:
A feature of note in Sinatra is it's ability to send responses back in different formats, such as ruby into HTML or to JSON. This simplifies our server side code and provides flexibility.
You can simply write string method into your Sinatra code for it to be output.
While a JSON strong can be created by utilizing the
.to_json method at the endo the code you want to output
This will be an ongoing blog as I learn more about the usages of Sinatra. For a further dive into the different routes with Sinatra, take a look at this link: