Reputation: 158
When I run an instance of WEBrick server and point my browser to localhost:2000/so_question, the server throws the following errors: i. ERROR /some_image.jpg not found. ii. ERROR /jquery.min.js not found.
sample code:
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
How to fix these errors? Plz, suggest a pure-Ruby solution(no gems, plz).
Thanks.
Upvotes: 0
Views: 193
Reputation: 173
You need to create a mount point to serve your statics file.
Put this line below after your s.mount "/so_question"
s.mount "/", WEBrick::HTTPServlet::FileHandler, './'
And then the Webrick will serve the statics file at ./
basedir
Upvotes: 1