Created
May 30, 2017 17:34
-
-
Save fkchang/01bf198fa95518224a60cb9427f88064 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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