Xirux Nefer Xirux Nefer - 4 months ago 9
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

gulp
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
gulpfile.js
is:

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

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


Basically, I have an
index.html
inside
./spec/fixtures/
folder and I am using the
browser-sync
module to start a local server, then I make it point to the
fixtures
folder inside my tests folder. When I want to run the tests, I have to do:

$ gulp server


in one terminal, then go to
http://localhost:4000
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
gulp
but using Ruby instead? I searched for a gem that worked in a similar way as the
browser-sync
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
gulp
, as it works perfectly, but it feels weird to mix JavaScript and Ruby in this particular way and in this context.

Answer

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. =]

Comments