Monday, March 18, 2013

Hello Nancy World in F#

As you may noticed (if you follow this blog or go to the same conferences as me) I really like the Nancy web framework. So naturally, since I've decided to finally learn F# properly, I threw together a hello world Nancy app in F#. It's very simple; the complete code is listed below. All I did was create an F# console application, install the Nancy and Nancy.Hosting.Self NuGet packages and type in this code, which will return the string "Hello" if you point your browser to "http://localhost:8888/nancy/":

So what's going in that code? Fist thing to notice is the type HelloModule which inherits NancyModule. On start up Nancy will pick up all NancyModules and run their constructor. In it's constructor HelloModule sets up a single route, "/", and tells Nancy what to do on a HTTP GET to that route, namely to execute a lambda that always returns "Hello". Lastly, since this is self hosted, there is a bit of setup in the "main" function: A Nancy host is instantiated, told where to listen for requests and started. This will make the Nancy self host start up Nancy and start listening for requests. That's it. Short and sweet.

No comments:

Post a Comment