F# and Nancy - beyond Hello World
I have been trying to find some examples on how to create a web app using F# with Nancy. Most of the tutorials on the web cover simple “Hello World!” app, only the basics. In this post, I want to go beyond “Hello World” and show real life examples.
F# and Nancy - Where to start ?
Most basic app “Hello World”, is simple.
Nancy will automatically load up the App class. To those familiar with Nancy on C# it looks almost the same. The only noticeable difference is the lambda function declaration, plus different syntax for inheritance. Also there is this weird smile face ‘:>’ at the end. This is just a up-casting operator. For some reason Get function has to return object. In F# you also use different syntax for list / array indexing. Instead of Get[”/”], you need to use Get.[”/”]
GET Examples
POST
To extract the parameters I had to cast the input param, which is of obj type, to Nancy.DynamicDictonary. It doesn’t look great but there is other way.
How to achieve syntax like that - parameters?name ?
This part of code is creating new “let-bound operator”. It hides the casting logic and makes the code look cleaner.
Error Codes
Views
Syntax is simple and looks basically the same as in C#. There is only one little detail. By default Nancy looks for views in /Views folder. Currently in VS F# project there is no way to create folders from within VS. In order to do this I had to manually, create folder, add file and edit *.fsproj file. Hopefully this will be fixed in the future.
ModelBinding
In C# syntax, you would normally use the base method Bind
The type to bind
And the code to bind to the model.
Unfortunately, it won’t work like that. My “TestModel” type is missing default parameter-less constructor and Nancy is throwing “No parameter less constructor defined for this object” Exception.
There is a constructor, no Exception, but the value is not bound and is always empty. To fix this, I had to go and look through
Nancy code. By default Nancy is binding to property. My val declaration is not a property.
Syntax for properties is a little different, but nothing serious here. I need to make my value mutable so I can modify the state.
The Application OnError pipeline
To modify the Pipeline I had to add include Nancy.Bootstrapper it has IApplicationStartup interface which can be used to hook into OnError pipeline
Nancy will automatically pick-up this class. The syntax for interface implementation is different, a lot. There is no += operator when working with Events and I had to use ADD method. With this example I got an exception “unable to resolve type AppStartup”. It was a problem of missing parameter less constructor.
The End
Those examples are not really showing the power of F#. This power lays in domain-business logic, not in simple endpoint declaration. It is also the OOP approach with F# syntax. There are other web frameworks that have more functional approach. You can check
Fancy which is a nice wrapper around Nancy. In a future, I might do a comparison with some purely functional web framework.