Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to run a local web server?

I can program and develop in Ruby on Rails/JS/HTML/CSS to make a full stack app. However, there are holes in my understanding of the HTTP request/response cycle. Are the following points correct?

  • If I make a Rails app, and on the command line type rails server I get a local server, which I can make requests to. If I open a browser, type localhost:3000, and press enter, I am making an HTTP request to the local server.
  • Rails uses by default a web server called WEBrick, though there are others like Thin, Puma, and Unicorn. These are all pieces of software, and what makes them web servers is the fact that the software implements functionality to process HTTP requests.
  • When I run a local web server, it means that my computer is running one of these pieces of software that listen for HTTP requests.

Is the above what it means "to run a local web server"?

  • I have seen other examples of ways to "run a local web server". One of the is to run npm install -g http-server in a project directory, and then navigate to localhost:8080. Is this also just software that starts running and accepts HTTP requests on port 8080?
  • On a Ruby command line, install rack gem: gem install rack. Then in a new Ruby file we require 'rack', start a web server:

Rack::Server.start({ app: MySimpleApp, port: 3000 })

We can then define a web application MySimpleApp that is rack-compliant (object that responds to call method):

class MySimpleApp
  def self.call
    (...)
  end
end

So now when we navigate in our browser to localhost:3000, MySimpleApp is executed. Is rack simply running it's default WEBrick server? Is what the above commands do simply run a local web server and define what to do when an HTTP request comes in (execute MySimpleApp)?

like image 415
evianpring Avatar asked May 11 '16 17:05

evianpring


2 Answers

You're pretty much right on your understanding there. HTTP is just a text-based protocol that, like many, operates over TCP/IP.

The built-in WEBrick server isn't the best example of an HTTP server written in Ruby, but it's included for legacy reasons because it's often "good enough" to get you started. Pow is considerably better and despite being produced by the same company that produced Rails it's largely written in Node.

The beauty of HTTP, like a lot of internet based protocols, is it doesn't matter what language you use so long as you comply with the standard.

Rack is a layer that operates behind HTTP and provides a thin layer of abstraction on the request/response cycle.

like image 99
tadman Avatar answered Oct 26 '22 06:10

tadman


A server is something that opens up a port (80, 443, 8080) for some sort of data transfer. Port 80 is the HTTP port and port 443 is the HTTPS port. 8080 is a commonly used port for development (as is 3000). https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

A local server by definition is a server running on your machine.

Overall, you are definitely on the right track.

like image 3
Beshoy Hanna Avatar answered Oct 26 '22 04:10

Beshoy Hanna