2017-04-04 79 views
0

当我运行的WEBrick服务器的实例,并指出我的浏览器到localhost:2000/so_question,服务器将引发以下错误: 我。未找到错误/some_image.jpg。 II。找不到ERROR /jquery.min.js。的WEBrick错误加载资源文件

示例代码:

require 'webrick'; include WEBrick 
s = HTTPServer.new Port: 2000 

class SoQuestion < HTTPServlet::AbstractServlet 

def do_GET req, resp 
    resp.status = 200 
    resp['Content-Type'] = 'text/html' 
    resp.body = prep_body 
end 

def prep_body 
    body = %(
    <!DOCTYPE html> 
    <html> 
    <head> 
    <title></title> 
    </head> 
    <body> 
) 
    body << '<p>Hello, world!</p>' 
    body << "<img src='some_image.jpg'>" 
    body << "<script src='jquery.min.js'></script>" 
    body << '</body></html>' 
end 

end 

s.mount '/so_question', SoQuestion 
trap('INT'){s.shutdown} 
s.start 

如何解决这些错误? PLZ,提出一个纯Ruby溶液(无宝石,PLZ)。

谢谢。

回答

1

你需要创建一个安装点以满足您的静态文件。

将这个线下方您s.mount "/so_question"

s.mount "/", WEBrick::HTTPServlet::FileHandler, './'

后再在WEBrick将服务于静态文件在./ BASEDIR

+0

谢谢,它解决了我的问题。 – ThunderThunder