lib/nitcorn: introduces a second action to show uri parameters usage
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 12 Dec 2014 02:43:14 +0000 (21:43 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 12 Dec 2014 22:41:38 +0000 (17:41 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

examples/nitcorn/src/nitcorn_hello_world.nit

index 7eba06b..a9e8f55 100644 (file)
@@ -50,10 +50,29 @@ class StaticAction
        end
 end
 
+# An action that uses parameterized uris to customize the output.
+class ParamAction
+       super Action
+
+       redef fun answer(http_request, turi)
+       do
+               var response = new HttpResponse(200)
+               var name = http_request.param("name")
+               if name == null then
+                       response.body = "No name..."
+               else
+                       response.body = "Hello {name}"
+               end
+               return response
+       end
+end
+
+
 var vh = new VirtualHost("localhost:8080")
 
 # Serve index.html with our custom handler
 vh.routes.add new Route("/index.html", new StaticAction)
+vh.routes.add new Route("/hello/:name", new ParamAction)
 
 # Serve everything else with a standard `FileServer` with a root at "www/hello_world/"
 vh.routes.add new Route(null, new FileServer("www/hello_world/"))