💡

what is Sinatra?

2024/12/03に公開

英語の勉強も兼ねて、英語で書いてます

sinatra is a web application frame work tool. by using this, it gets easier to deveop web applications because it handles HTTP requests, define routes and so on.

Unlike heavier frameworks, like Rails, Sinatra is simple to use and suitable to develop a small application.


why do I need to run this in the terminal in stead of vs code?

#!/usr/bin/env ruby

require 'sinatra'#use a gem called sinatra

get '/' do
    'Hello, world!'
    end

In terminal, I need to run chmod +x main.rb and then run the file name which is .main.rb ,right? Why do I need to run these in terminal?

you can run the code directly using chmod +x main.rb without calling Ruby(ruby main.rb).
if you execute this properly, you get this!

get '/' do
    'Hello, world!'

this code means, if i access to the root directory, it gives us Hello, world!.

root directory is the starting point of the web application.
This route defines what content to return when users visit the app's main page.

③what is webrick server?
it is a server.


require 'sinatra/reloader'
by using this, we no longer need to restart and reset the server.

get '/hello' do
    'hello, world!'
end

get '/goodbye' do
    'goodbye, world!'
end

the content that displayed on the web page will be changed by the end of the link.

get '/hello/:member' do
    puts "member => #{params}"
    'hello, world!'
end

params is a variable. member is a key

and the process of this is like this. I can see this in terminal.

member => {"member"=>":brother"}
::1 - - [03/Dec/2024:20:21:24 +0900] "GET /hello/:brother HTTP/1.1" 200 13 0.0118


memeber is a key and brother is a value.

get '/calc/:formula' do
    f = params[:formula]
    "#{f} = " + eval(f).to_s

eval:run the code and make the phrase to strings and add that num with the {f}

Discussion