-
Notifications
You must be signed in to change notification settings - Fork 8
/
express-wrapper.rb
50 lines (43 loc) · 1.06 KB
/
express-wrapper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'native'
class Express < Native::Object
EXPRESS = `OpalNode.node_require('express')`
def initialize &block
# `console.log(typeof #{EXPRESS});`
@native = `#{EXPRESS}()`
p app.class
# `console.log(#{app}._klass);`
p Kernel.native?(app)
# app = super(app)
yield self if block_given?
end
class Request < Native::Object
end
class Response < Native::Object
alias_native :send, :send
alias_native :[]=, :setHeader
end
%i[get post put delete].each do |method_name|
define_method method_name do |path, &block|
%x|
#{@native}[#{method_name}](#{path}, function(req, res) {
req = #{Request.new(`req`)};
res = #{Response.new(`res`)};
#{block.call(`req`, `res`)}
})
|
end
end
end
Express.new do |app|
app.get '/' do |req, res|
p req.class
p res.class
res.send 200, <<-HTML
<!doctype html>
<html>
<head><style>body{font-family: sans-serif;}</style></head>
<body><h1>Hulla!</h1></body>
</html>
HTML
end
end.listen 3000