Reputation: 317
My designer recently provided me with new design folder with different pages and mentioned the following:
You need to serve the dist folder with http server. For example: python -m SimpleHTTPServer 8000 open http://localhost:8000 http://localhost:8000/listing.html and http://localhost:8000/detail_view.html for the different pages
How do I accomplish this with Rails? or Ruby?
Upvotes: 3
Views: 7493
Reputation: 549
You could try with ruby -run -e httpd . -p 8000
, which will start a WEBrick server on your current directory.
Check this link for more info: http://sweetme.at/2013/08/28/simple-local-http-server-with-ruby/
Upvotes: 10
Reputation: 19221
If I'm understanding you correctly, you're wondering about serving static files while using Ruby.
I'm adding this answer because the built-in server that comes as part of the Ruby Standard Library (WEBrick) isn't optimal for production environments or heavier loads (i.e. larger files).
In general, most production environments use the networking stack a bit differently. Often the nginx / Apache layer will handle static files while the Ruby server will handle dynamic content.
However, to make life easier (at the expense of resources), most major Ruby frameworks (Rails, Sinatra, etc') support static file serving as well as dynamic functionality.
You can either use a framework or look into Rack (which is the platform used by most frameworks) to serve static files.
Also, some Ruby servers, such as iodine
(I'm the author) support static file serving...
Here's an approach that uses Rack directly and can be used with most Ruby servers (such as puma
, iodine
, thin
, etc').
Here's a simple Rack application, save the following in a file named config.ru
at the root of your application:
# Our app will simply return a 404 not found code
RESPONSE = [404, { 'Content-Type'.freeze => 'text/html'.freeze,
'Content-Length'.freeze => '14'.freeze }.freeze,
['File Not Found'.freeze]].freeze
# This is the application object
app = proc do |_env|
RESPONSE
end
# We will use the Rack static file service middleware.
# You might want to update the folder name.
use Rack::Static, :root => 'public'
run app
make sure to install the ruby server gem from the command line run... you can use any of the following:
gem install puma
# or
gem install iodine
# or
gem install thin
Next, simply run the server from the command line (in the folder where your ruby application config.ru
is placed). i.e.:
puma -p 8888
# or
iodine -p 8888
P.S.
Having said that... a web server such as nginx or apache is probably the best tool for the job.
Barring that, you could probably use iodine
for unencrypted (no SSL) static file serving with (remember to install first):
iodine -www ./public
You don't need a Ruby application for this, You'll just be running a Ruby application server from the command line without giving it any Ruby application to run.
Upvotes: 0
Reputation: 3568
Maybe look at spinning up a Webrick server with a document root of the HTML, CSS and JavaScript static assets folder (preferably called public).
require 'webrick'
server = WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: "/var/www/app/public")
server.start
The one liner equivalent:
ruby -rwebrick -e'WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: "/var/www/app/public").start'
Which is just the Ruby version of the Python code provided by your front-end guy.
Maybe just use Python, honestly it wouldn't really matter.
http://ruby-doc.org/stdlib-2.0.0/libdoc/webrick/rdoc/WEBrick.html
http://tobyho.com/2009/09/16/http-server-in-5-lines-with/
In production environments, concurrency is a necessity.
WEBrick has been strongly unrecommended by providers like Heroku because by default it behaves as a single thread when used by Rails.
https://devcenter.heroku.com/articles/ruby-default-web-server
But WEBrick itself is a multithreaded webserver.
https://github.com/rails/rails/issues/10772
Is puma the ONLY multi-threaded rails 4 http server?
Upvotes: 3
Reputation: 102
Once you have created a new rails app with:
rails new your_app
Just type rails s
or rails server
into the terminal. This will launch a server on localhost:3000.
Upvotes: 2