Skip to content

Instantly share code, notes, and snippets.

@fkchang
Created May 30, 2017 17:34
Show Gist options
  • Save fkchang/01bf198fa95518224a60cb9427f88064 to your computer and use it in GitHub Desktop.
Save fkchang/01bf198fa95518224a60cb9427f88064 to your computer and use it in GitHub Desktop.
require 'nodejs'
require 'native'
module HTTP
HTTP_JS = Native(node_require('http'))
class Server
def self.listen(port, &block)
HTTP_JS.createServer(lambda { |req_js, res_js|
req_opal = Native(req_js)
res_opal = Native(res_js)
status, headers, body = block.call(req_opal, res_opal)
res_opal.writeHead(status, headers.to_n)
res_opal.end(body)
}).listen(port)
end
end
end
port = 1337
HTTP::Server.listen(port) { |req, res|
[200, {'Content-Type': 'text/plain'}, "Hello World\n"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment