Crystal-语言服务index.html
Crystal-lang serve index.html
我对这门语言有点陌生,我想开始研究一个非常简单的 HTTP 服务器。我当前的代码如下所示:
require "http/server"
port = 8080
host = "127.0.0.1"
mime = "text/html"
server = HTTP::Server.new(host, port, [
HTTP::ErrorHandler.new,
HTTP::LogHandler.new,
HTTP::StaticFileHandler.new("./public"),
]) do |context|
context.response.content_type = mime
end
puts "Listening at #{host}:#{port}"
server.listen
我的目标是不想列出目录,因为这样就可以了。如果 public/
可用,我实际上想提供 index.html
,而不必将 index.html
放在 URL 栏中。让我们假设 index.html
实际上确实存在于 public/
。任何指向可能有用的文档的指针?
是这样的吗?
require "http/server"
port = 8080
host = "127.0.0.1"
mime = "text/html"
server = HTTP::Server.new(host, port, [
HTTP::ErrorHandler.new,
HTTP::LogHandler.new,
]) do |context|
req = context.request
if req.method == "GET" && req.path == "/public"
filename = "./public/index.html"
context.response.content_type = "text/html"
context.response.content_length = File.size(filename)
File.open(filename) do |file|
IO.copy(file, context.response)
end
next
end
context.response.content_type = mime
end
puts "Listening at #{host}:#{port}"
server.listen
我对这门语言有点陌生,我想开始研究一个非常简单的 HTTP 服务器。我当前的代码如下所示:
require "http/server"
port = 8080
host = "127.0.0.1"
mime = "text/html"
server = HTTP::Server.new(host, port, [
HTTP::ErrorHandler.new,
HTTP::LogHandler.new,
HTTP::StaticFileHandler.new("./public"),
]) do |context|
context.response.content_type = mime
end
puts "Listening at #{host}:#{port}"
server.listen
我的目标是不想列出目录,因为这样就可以了。如果 public/
可用,我实际上想提供 index.html
,而不必将 index.html
放在 URL 栏中。让我们假设 index.html
实际上确实存在于 public/
。任何指向可能有用的文档的指针?
是这样的吗?
require "http/server"
port = 8080
host = "127.0.0.1"
mime = "text/html"
server = HTTP::Server.new(host, port, [
HTTP::ErrorHandler.new,
HTTP::LogHandler.new,
]) do |context|
req = context.request
if req.method == "GET" && req.path == "/public"
filename = "./public/index.html"
context.response.content_type = "text/html"
context.response.content_length = File.size(filename)
File.open(filename) do |file|
IO.copy(file, context.response)
end
next
end
context.response.content_type = mime
end
puts "Listening at #{host}:#{port}"
server.listen