whack-a-node
Node apps running in any rack server as a endpoint, and potentially a middleware layer.
Leverage authentication, authorization and more in front of your node apps, letting node be used for high concurrency portions of your app.
Reach a node app as an RPC directly from your Rails models (coming soon)
Prerequisites
-
Ruby 1.8.7 or 1.9.2 work fine
-
Node.js
brew install node (or equivalent for your environment)
-
Rack (or Rails 3)
Installation
sudo gem install whack-a-node
Usage
In Rails 3’s routes.rb:
match “/proxy” => WhackANode::Proxy.new(“/”)
This will attempt to reach a node server on localhost at port 8810. The response will be proxied by Rack.
match “/redirect” => WhackANode::Redirect.new(“/”,“www.example.com”, 2306)
This will redirect to a node server at www.example.com on port 2306. In some cases, this can result in faster performance, as you don’t have to have a Rack instance sitting around for the whole request.
Generally speaking, using the proxy over a Rails controller ends up being about 8-10 times faster. While still much slower than Node directly, this can still yield benefit.
Examples
Node server code
server.js:
var http = require(‘http’); http.createServer(function (req, res)
res.writeHead(200, {'Content-Type': 'text/plain');
res.end('Hello World\n');
}).listen(8810, “127.0.0.1”); console.log(‘Server running at 127.0.0.1:8810/’);
Start the node server by running node server.js
Rails server
Create a new Rails app. Add the following to routes.rb
match “/proxy” => WhackANode::Proxy.new(“/”) – WhackANode defaults to localhost port 8810, so this will work with above server Add Whack-A-Node to your Gemfile:
gem ‘whack-a-node’
Start your Rails app. Hit localhost:3000/proxy. You should see ‘Hello World’ generated by Node.js
Contributing to whack-a-node
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2011 Niels Meersschaert & Matthew Jording. See LICENSE.txt for further details.