【问题标题】:WEBrick error loading asset filesWEBrick 加载资产文件时出错
【发布时间】:2017-04-04 06:15:52
【问题描述】:

当我运行 WEBrick 服务器实例并将浏览器指向 localhost:2000/so_question 时,服务器会抛出以下错误: 一世。错误 /some_image.jpg 未找到。 ii.错误 /jquery.min.js 未找到。

示例代码:

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

如何解决这些错误?请建议一个纯 Ruby 解决方案(没有宝石,请)。

谢谢。

【问题讨论】:

    标签: ruby webrick


    【解决方案1】:

    您需要创建一个挂载点来提供您的静态文件。

    将此行放在您的s.mount "/so_question" 之后

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

    然后 Webrick 将在 ./ basedir 处提供静态文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 2017-09-20
      • 2016-09-13
      相关资源
      最近更新 更多