To use the built-in HTTP server, you need to include the
net/simple_server library in your Goby file.
To create an instance of the simple server, use the class
Net::SimpleServer and then specify the port number.
server = Net::SimpleServer.new(3000) # Create a server instance with port 3000
Goby's simple server support for different kinds of HTTP verb including the
DELETE actions which enables you to create simple RESTful application. Let's say we want to create a root path with a
server.get('\') do |request, response| response.status = 200 response.body = 'Hello Visitor!' response.set_header('Content-Type', 'text/plain') end
You can see that we can specify our route and pass in whatever kind of HTTP verbs as a method of the server instance. It provides the
Net::HTTP::Request and the
Net::HTTP::Response objects to help you construct the response of the request.
In the example code above, the status of the response is specified as 200 and the content is the string provided to the body of the response. Notice that to set the header, we use the
Net::HTTP::Response#set_header method instead of passing a hash like object to set the response header.
After you've set all of the routings, the you can simply start the server using the
server.start # Start the HTTP simple server in port 3000
Go to the http://localhost:3000/ to see whether the server is saying hi to you!