For some time I’m switching all my development from C# to F# (because of reasons…), and next on the list was Web development. Attempts at using ASP.NET MVC combined with F# were failures, mostly because I only switch language, not a general approach and still tried to use Razor, AutoMapper, Castle.Windsor and nHibernate, and as such I was bound to fail. Recently I came across Nancy and React, and decided to give them a try. For starters I recommend F# and Nancy – beyond Hello World by Michał Franc, I’ll add few notes to that.
Static content and folders
Though you may create folders for static content in your F# project in VS with F# power tools, it’s pain in the ass to work with them, manually sorting folders contents in *.fsproj file isn’t the most productive way to start work when reopening visual studio after some minor project structure changes. What I also noticed is VS slowing down.
VS Code and symbolic link
To avoid using folders I prefer to use VS code for non F# coding (CSS, JS, so on) and creating symlinks to static content in order to avoid manually coping files after change, or creating additional build scripts.
for bootstrapper and module looking like:
type Bootstrapper() as __ = inherit DefaultNancyBootstrapper() do #if DEBUG Nancy.StaticConfiguration.DisableErrorTraces <- false #endif override __.ConfigureConventions nancyConventions = base.ConfigureConventions(nancyConventions) nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("/Scripts",null)) nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("/Content",null)) nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("/App",null))
type App() as this = inherit NancyModule() do this.Get.["/"] <- fun _ -> this.View.["Index.html"] :> obj
we’ll need following symlinks:
mklink Index.html "..\..\..\Static\Index.html" mklink /D Content "..\..\..\Static\Content" mklink /D App "..\..\..\Static\App" mklink /D Scripts "..\..\..\Static\Scripts"
In order to create them, open Command Prompt as Administrator and use mklink command, supplying link name as first argument, and path to actual file/directory as second.
Note: I only create symlink to one view, Index.html, as in following posts I’ll be using React, and single view is sufficient.
Running our application
Self Hosted Nancy
Fastest way to run application is to install Nancy.Hosting.Self NuGet package and run in console
[<EntryPoint>] let main _ = let nancy = new Nancy.Hosting.Self.NancyHost(new Bootstrapper(), new Uri("http://localhost:8100")) nancy.Start() while Console.ReadLine() <> "x" do () 0
Source code, for this and posts that will follow available at: