0

編集:興味深いものを見つけました。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 ファイルの場所が間違っていました。

4

1 に答える 1

0

コメントで「mu が短すぎる」と指摘しているように、私はすべてを で送り返す初心者の大きな間違いを犯しましたContent-type: text/html。これは明らかに大きな問題です! 適切なファイルを適切なコンテンツ タイプに関連付けるだけで、すべてが機能します。固定server.rbファイルは次のとおりです。

require 'socket'

def start(args)
   webserver = create_server args
   basepath = './app/'

   while (session = webserver.accept)
      request = session.gets
      trimmedrequest = trim_request(request)
      ct = get_content_type trimmedrequest
      session.print "HTTP/1.1 200/OK\nContent-type:#{ct}\n\n"
      puts"HTTP/1.1 200/OK\nContent-type:#{ct}\n\n" 
      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 

def get_content_type(path)
    ext = File.extname(path)
    return "text/html"  if ext.include? ".html" or ext.include? ".htm"
    return "text/plain" if ext.include? ".txt"
    return "text/css"   if ext.include? ".css"
    return "image/jpeg" if ext.include? ".jpeg" or ext.include? ".jpg"
    return "image/gif"  if ext.include? ".gif"
    return "image/bmp"  if ext.include? ".bmp"
    return "text/plain" if ext.include? ".rb"
    return "text/xml"   if ext.include? ".xml"
    return "text/xml"   if ext.include? ".xsl"
    return "text/html"
end
于 2013-07-01T19:51:28.553 に答える