0.6.4 • Published 10 years ago

opal v0.6.4

Weekly downloads
16
License
-
Repository
github
Last release
10 years ago

Opal for Node.js

Build Status

Use Ruby on Node.js for REAL-WORLD rofl-SCALING

Usage

Run with opal-node app.rb or run the repl: opal-node-irb

Installing

Install with NPM: npm install -g opal or via RVM: rvm install opal

Example (rack)

# app.rb
require 'http/server'
HTTP::Server.start 3000 do
  [200, {'Content-Type' => 'text/plain'}, ["Hello World!\n"]]
end

This is the original Node.js example:

// the original nodejs example
http = require('http')
var port = process.env.port || 1337;
http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port);

Here's the (naïve) rack-like implementation of http/server.rb

# http/server.rb
module HTTP
  `_http = require('http')`
  class Server < `_http.Server`
    alias_native :listen, :listen

    def self.start port, &block
      server = new `function(request, response) {
        request.on('end', function(chunk) {
          var rackResponse = #{ block.call(`request`, `response`) };
          response.writeHead(rackResponse[0], #{ `rackResponse[1]`.to_native });
          response.end( rackResponse[2].join(' ') );
        })
      }`
      server.listen(port)
      server
    end
  end
end

Example 2 (express.js wrapper)

A simple Express.js wrapper example can be found in examples/express-wrapper.rb

Express.new do
  get '/' do |req, res|
    res.send 200, 'hulla!'
  end
end.listen 3000

Developing

Start a coffee watcher to keep opal.js in sync with opal.coffee

coffee -cwo lib/ lib/*.coffee

Running in development

NODE_PATH="$NODE_PATH:../lib/" node ./bin/opal-node
NODE_PATH="$NODE_PATH:../lib/" node ./bin/opal-node-irb

Testing

To keep the specs running while developing just install and use spectator that will compile opal.coffee to opal.js before every run of the spec.

$ gem install spectator
$ gem install notify # to get notification
$ spectator
--- Waiting for changes...

License

This project rocks and uses MIT-LICENSE.

0.6.4

10 years ago

0.6.3

10 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.5

10 years ago

0.3.3

11 years ago

0.3.2

12 years ago

0.3.1

12 years ago

0.3.0

12 years ago

0.2.0

12 years ago

0.1.4

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.0

12 years ago