编辑:发现了一些有趣的东西。CSS 适用于 IE8,但不适用于 Firefox。我猜这是字符编码问题?
我正在尝试学习如何编写自己的 Web 服务器,并且我设法让服务器启动并运行,但由于某种原因它不会显示 css。然而,Javascript 工作得很好。css 文件已加载,但样式未显示在 Firebug 中。谁能告诉我我在这里做错了什么?
服务器的代码如下所示。要运行服务器,您只需调用“启动”一个空列表。或者,如果需要,您可以在此处提取整个代码库:https ://github.com/dm9600/webserver
require 'socket'
def start(args)
webserver = create_server args
basepath = './app/'
while (session = webserver.accept)
puts "HTTP/1.1 200/OK\nContent-type:text/html\n\n"
session.print "HTTP/1.1 200/OK\nContent-type:text/html\n\n"
request = session.gets
puts "request" + request
trimmedrequest = trim_request(request)
filename = trimmedrequest.chomp
begin
displayfile = find_file(filename)
content = displayfile.read()
session.print content
rescue Errno::ENOENT
session.print "File not found"
end
session.close
end
end
def create_server(args)
command = args[0]
#default port is going to be 3333
port = 3333
#default address will be localhost
address = "localhost"
port = args[1] if args[0].instance_of? String and args[0].eql? "p"
puts "Server created at #{address} and port #{port}"
TCPServer.new address, port
end
def trim_request(request)
request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
end
def find_file(path)
basepath = "./app/"
if path.empty?
full_path = basepath + 'index.html'
else
full_path = basepath + path
end
File.open full_path, 'r'
end
我正在运行的 html。它位于 /app/index.html 中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="js/application.js"></script>
<title>CSS-Test</title>
<link type="text/css" href="css/application.css" rel="stylesheet"></link>
</head>
<body>
<h1>CSS-Test</h1>
<div id="box-one">
<p>This is box one.</p>
</div>
<div id="box-two">
<p>This is box two.</p>
</div>
</body>
</html>
我正在尝试运行的 css。它位于 /app/css/application.css
.someclass {
background: blue;
}
* {
background: black;
}
编辑:这里有错误的 css 文件位置。