As a first simple example of using Luca I wanted to write a simple web service that returns json or plain text. Keep in mind that Luca is extremely “Alpha” at this moment and some of the API‘s will surely change in the future.
If you want to try it and follow this example along, you will need to download and compile Luca from Github.
Getting right into it.
Create a new IIS application and make sure that the Application Pool is set to “ASP.NET v4.0″.
Copy all the dll’s from Luca.Core into a bin folder inside the newly created IIS application.
Add a web.config with the following content at the root level.
The folder structure of your website should look something like this now.
Open the main.js file and register your first handler function.
We are registering in the global object app a function to handle a GET request to a url that match the regular expression passed as the first argument.
This is similar to the way Sinatra works in Ruby of Express in node.js. It’s not really routing per se, but you can think about it a similar way.
The object “app” also have methods to handle the other HTTP verbs as well, like Post, Put and Delete. The signature is the same in all cases.
When we call the function handler we will pass the request object as the only parameter. This is not the HttpRequest object but a Luca request object. I will explore this object in a future post. In this example we are reading a parameter called id passed into the query string.
In this case, we are registering a function to handle a call to a route that match “/talk”. The function will try to read a query string parameter with the key “greet” and output a greeting to the screen.
Luca will check for the Accept header and try to find a Encoder that knows how to handle that type of content. If it can’t find an adecuate handler, will use the default encoder (TextEncoder). At the moment, Luca pays no attention to the extension passed in the url for the purposes of encoding.
Luca will come with a JsonEncoder (already in the code), a plain/text encoder (in the code but partially functional), an Xml encoder and an HtmlEncoder.
In future posts I will look at the Request object, Encoders and configuration options.