Reputation: 745
I'm a newbie to Crystal-lang.
I'm trying the Http Server example given in the crystal-lang docs.
require "http/server"
server = HTTP::Server.new(8080) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.now}"
end
puts "Listening on http://127.0.0.1:8080"
server.listen
When I run this code I'm getting the following error.
Error in httpserv.cr:3: no overload matches 'HTTP::Server.new' with type Int32
Overloads are:
- HTTP::Server.new(handlers : Array(HTTP::Handler), &handler)
- HTTP::Server.new(&handler)
- HTTP::Server.new(handlers : Array(HTTP::Handler))
- HTTP::Server.new(handler : HTTP::Handler | HTTP::Handler::Proc)
server = HTTP::Server.new(8080) do |context|
What I can gather from this is I need to specify some methods/functions for handling the HTTP requests, but I'm not getting how to do that.
What is the correct way to do that is my question?
Thanks in advance.
Upvotes: 4
Views: 257
Reputation: 5661
The HTTP::Server
API has changed in the latest release Crystal 0.25.0. It's use is documented in the recent API docs (the 0.25.0 release docs have a slight error, but it's fixed on master).
As you can see from the error message, the signature of Server.new
has changed. It now only accepts some form of handler. You already have that defined in your example in the do
block. The constructor no longer accepts a port number, but you need to call #bind_tcp
seperately.
Your example should now look like this:
require "http/server"
server = HTTP::Server.new do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.now}"
end
address = server.bind_tcp 8080
puts "Listening on http://#{address}"
server.listen
Upvotes: 5