testing this little express js task one of my friends got
This commit is contained in:
parent
37d242cde1
commit
b20244e2e5
|
@ -0,0 +1,32 @@
|
|||
require_relative "hyde"
|
||||
server = Hyde::Server.new Port: 8000 do
|
||||
{"add" => -> (a,b) { a + b },
|
||||
"sub" => -> (a,b) { a - b },
|
||||
"mul" => -> (a,b) { a * b },
|
||||
"div" => -> (a,b) {
|
||||
begin
|
||||
return a/b
|
||||
rescue ZeroDivisionError
|
||||
return "Divided by zero"
|
||||
end
|
||||
}
|
||||
}.each_pair do |k,v|
|
||||
serve k do |ctx|
|
||||
req,res = ctx.request,ctx.response
|
||||
a,b = req.query["a"],req.query["b"]
|
||||
result = (a and b) ? v.call(a.to_f,b.to_f) : "Invalid parameters"
|
||||
res.body = "
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title> Calculator API test </title>
|
||||
</head>
|
||||
<body>
|
||||
<h> Result: #{result} </h>
|
||||
</body>
|
||||
</html>"
|
||||
res['Content-Type'] = "text/html"
|
||||
end
|
||||
end
|
||||
end
|
||||
server.start
|
Loading…
Reference in New Issue