Xirux Nefer Xirux Nefer - 1 year ago 43
Ruby Question

set up a basic local server with ruby

This is maybe a noobie question, but I am working on a Ruby project and now I have to serve some files from localhost for a couple of tests that need to read a webpage that is loaded locally. I am using

for that, but I would like to do it in Ruby. I found out about Rack but I can't wrap my head around it to do the very simple thing I want to do.

This is my folder structure:

$ tree
├── lib
│   └── page_reader.rb
├── spec
│   ├── fixtures
│   │   └── index.html
│   └── page_reader_spec.rb
└── gulpfile.js

What I have now in my

browsersync = require('browser-sync').create(),
gulp = require('gulp')

gulp.task('server', function() {
server: {
baseDir: './spec/fixtures/'
port: 4000,
notify: false,
open: false

Basically, I have an
folder and I am using the
module to start a local server, then I make it point to the
folder inside my tests folder. When I want to run the tests, I have to do:

$ gulp server

in one terminal, then go to
to load the page against which I am running the tests, and then in another terminal do:

$ bundle exec rspec

to run the tests.

Is there a way to do what I am doing through
but using Ruby instead? I searched for a gem that worked in a similar way as the
module but couldn't find any. However I think there must be a way that is as simple as it is doing it with gulp. I can keep on doing it with
, as it works perfectly, but it feels weird to mix JavaScript and Ruby in this particular way and in this context.


Simplest answer I know:

cd spec/fixtures && ruby -run -e httpd -- -p 4000 .

That's courtesy of Aaron Patterson (@tenderlove), via Gavin Kistner, Ben Oakes, and a cast of thousands. I love a good community. =]