One of the constant annoyances I seem to run into is the need for absolute paths in my web work. Whether it's publishing an HTML layout with the need to reference all of the images from the root directory (e.g. '/images/') or building a Flash app with the same need, I seem to run into quite a few situations where you never know what directory the final product will end up in and relative paths just won't do.
Previously, I'd taken the approach of using relative paths in development and then changing the references just before doing a final test on a development web server. While this works fine for the initial round of work, it becomes pretty tedious to keep switching back & forth as you move through changes and revisions. One of the things I've always liked about Ruby on Rails is the ability to run a built in web server for local development, which entirely eliminates these issues. Rails will run a web server called Mongrel if installed, but (I believe) still falls back to a pure ruby server called Webrick. Webrick, however has no need for rails in order to run (though it does require Ruby) and makes a wonderful little HTTP server for down & dirty development.
If you are on a Mac you've already got Ruby installed and this should work. If you're on Windows you can install Ruby but you're on your own on that one. Once installed you'll want to create a quick ruby script and set to be executable. The contents of your script would be as follows:
#!/usr/bin/ruby
require 'webrick'
include WEBrick
httpath = File.expand_path(File.dirname(__FILE__))
puts("Serving: " + httpath)
s = HTTPServer.new(
:Port => 2000,
:DocumentRoot => httpath
)
trap("INT"){ s.shutdown }
s.startThe httpath variable is set to the directory containing the script itself, so you'd essentially place the script inside of whichever folder you want to be your document root. The port is set to 2000. I include this file in each of the projects I create and when working I can just quickly run the script (I call the file 'servthis') and point my browser to 'localhost:2000'. Now I'm free to use absolute paths to images or XML files in my code without sending files over FTP to a "real" server.

Comments
Post new comment